I have a simple translation function that takes one parameter(string) and return string. It works fine when i use like await translation(key)
how ever my need it to use it frequently inside in react native component like below
<Text>{tranlate("faqs")}</Text>
Then again somewhere
<Button title={translate("terms")}/>
My question is although its a async function and not trigger without await or then/catch block. What could be other way i can simple use it without await or then/catch because using it like below is not working
<Text>{await translate("faqs")}</Text>
Any help would be really appriciated!Thanks
2
Answers
You can use a simple custom hook that will return you the translated text:
This can then be consumed in a component like this:
It’s better to create a custom hook which takes
key
as input and return translated key.You shouldn’t use
async-await
in(J|T)SX
.CODESANDBOX