<Branch>
API Reference for the <Branch> component
Overview
The <Branch>
component allows you to add conditional logic to a translation.
You pass a value to the branch
parameter, and this gets matched with an output value based on the keys you provide.
Reference
Props
Prop | Type | Default |
---|---|---|
branch | string | - |
children? | any | undefined |
name? | string | undefined |
[key]: string | string | JSX.Element | - |
The [key]: string
syntax indicates arbitrary keys representing potential branches.
For example, branches like active
and inactive
can be added as parameters.
Prop | Description |
---|---|
branch | The name of the branch to render. |
children | Fallback content to render if no matching branch is found. |
name | Optional name for the component, used for a dictionary design pattern. |
[key]: string | Branches representing possible content for the given branch value. Each key corresponds to a branch, and its value is the content to render. |
Returns
JSX.Element
containing the content corresponding to the specified branch or the fallback content.
Throws
Error
if the branch
prop is not provided or is invalid.
Examples
Basic usage
<Branch>
needs to have a different output for each possible value of the branch
prop.
In this example, the user.hairColor
value is used to determine the output.
We have defined props black
, brown
, and blonde
to match the possible values of user.hairColor
.
Fallback content
The children
will always be used as a fallback if no prop matches the value passed to branch
.
Translating <Branch>
If you want to translate the content, simply wrap it in a <T>
component.
Adding variables
If you want to render dynamic values in the branch, make sure to wrap them in <Currency>
, <DateTime>
, <Num>
, or <Var>
components.
Using a dictionary
Basic usage
Passing values to dictionary entries
In order to pass values, you must specify a name for the <Branch>
(or other) component(s) and reference it when calling the t()
function.
In this example, we see that hairColor
is passed to both <Branch>
and <Var>
components.
Notes
- The keys for branches can be any string value that matches the branch prop. This flexibility makes it easy to adapt
<Branch>
to a wide range of use cases. - Combine
<Branch>
with other components, such as<T>
for translations and variable components for dynamic content, to create rich and localized interfaces.
Next steps
- For more advanced usage and examples, refer to Using Branching Components.
- To learn more about variable components like
<Currency>
,<DateTime>
,<Num>
, and<Var>
, see the Using Variable Components documentation.