useElement()
API Reference for the useElement hook
Overview
The useElement()
method allows you to access jsx content from your translation dictionary.
useElement()
supports:
- Jsx translation only (no string support, use
getGT()
,useGT()
, ortx()
for string translations). - Variable insertion and conditional logic within translations.
- Optional id prefixing.
- Identical syntactic implementation on both client and server side.
getGT()
, useGT()
, and useElement()
use a dictionary to store all content for translation.
This is different from using the <T>
component for translation.
If you are interested in only using <T>
components for translation, then this document is not relevant.
Despite its name, useElement()
is not always a hook.
When used on the client side, it is implemented as a hook.
When used on the server side, it synchonously resolves the translations and streams the result.
useElement()
excludes strings as this allows for identical syntax in both client and server side components.
For accessing both strings and jsx content, use getGT()
or useGT()
.
Reference
Props
Prop | Type | Default |
---|---|---|
id? | string | undefined |
Description
Prop | Description |
---|---|
id | An optional prefix to prepend to all translation keys. This is useful for working with nested dictionary values. |
Returns
A translation function t
with the following signature:
key
: The identifier of the translation to fetch.- Returns: The translated value as a
React.ReactElement
.
Examples
Basic Usages
Every entry in your dictionary gets translated.
useElement()
can only resolve jsx entries.
Strings are forbidden.
When we want to access these entries, we call useElement()
.
This returns a function that accepts the key of a translation from the dictionary.
Using variables
You can pass variables to dictionary translations. Just use a variable component.
Whenever you have a variable in your jsx translations,
NEVER insert variables directly into the jsx.
ALWAYS use a variable component:
<Currency>
, <DateTime>
, <Num>
, or <Var>
.
In order to pass values, you must (1) assign an identifier and (2) reference the identifier when calling the t()
function.
In this example, we use <Var>
to pass a variable to the translation.
In the dictionary, we assign <Var>
the identifier name="userName"
.
In the component, we pass the value by invoking the identifier userName: "Alice"
.
Using branches
Branching components, such as <Branch>
and <Plural>
,
can be used to handle control logic in a translation.
In order to pass values, you must (1) assign an identifier and (2) reference the identifier when calling the t()
function.
In this example, we see that hairColor
is passed to both <Branch>
and <Var>
components.
Using prefixes
We can use prefixes to only translate a subset of the dictionary.
Because we added the value 'prefix1.prefix2'
to the useElement()
method, all of the keys are prefixed with prefix1.prefix2
:
Notes
useElement()
only translates JSX content from the dictionary. No strings.useElement()
is "platform agnostic", meaning it can be used in both client-side and server-side components.- For string-based translations, consider using
useGT()
,getGT()
, ortx()
.
Next Steps
- Read more about the dictionary design pattern.
- Combine
useElement()
with Variable Components and Branching Components for advanced translation scenarios.