React Hooks support
See original GitHub issueI’d like to start a discussion about react hooks in lingui-react. I know that at this moment is just in alpha, but maybe is time to start thinking about a better alternative of i18n renderprops.
IMO, I don’t like this alternative:
function Example(){
return (
<I18n>
{({ i18n }) => (
<img src="..." alt={i18n._(t`Image caption`)} />
)}
</I18n>
)
}
The reasons are:
- So much code to just one thing
- Renderprops are creating an extra useless wrapper
- Having a lot of renderprops like this is not so much readable, it’s like callback hell…
A better alternative can be:
function Example(){
const alt = useT('Image caption')
return (
<img src="..." alt={alt} />
)
}
What do you think about this?
Issue Analytics
- State:
- Created 5 years ago
- Comments:12 (5 by maintainers)
Top Results From Across the Web
Introducing Hooks - React
React 16.8.0 is the first release to support Hooks. When upgrading, don't forget to update all packages, including React DOM. React Native has...
Read more >React Hooks - W3Schools
Hooks allow function components to have access to state and other React features. Because of this, class components are generally no longer needed....
Read more >useHooks - Easy to understand React Hook recipes
Hooks are a feature in React that allow you use state and other React features without writing classes. This website provides easy to...
Read more >React Hooks Tutorial – useState, useEffect, and How to ...
Hooks were first introduced in React 16.8. And they're great because they let you use more of React's features – like managing your ......
Read more >The Guide to Learning React Hooks (Examples & Tutorials)
First released in October of 2018, the React hook APIs provide an alternative to writing class-based components, and offer an alternative approach to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey @aralroca, thank you for suggestion. I already mentioned hooks briefly in my talk at React Conf 2018, but let’s give it a proper discussion here.
In upcoming v3 we’re planning to abandon both HOC and render prop component. They both have the same purpose: consume i18n object from context and provide it to child component. Question is (and this question was asked few times, e.g: #46), why can’t we access i18n object directly, like so:
The only reason is re-rendering - when you change the active language or re-load message catalogs, ideally you would like to re-render your translations. HOC and render prop component take care of this. However, we figure out that in practice this happens rarely and when you switch the locale, it’s safe to re-render whole app.
So, instead of context we’re planning to use global i18n object:
t
macro is transformed intoi18n._
call, wherei18n
is imported from@lingui/core
. There’s gonna be an escape hatch in macro configuration if you don’t like to use globali18n
object (e.g. if you prefer to use your own instance of i18n for whatever reason).Hooks
To sum up previous section, there won’t be no HOC or render props in next version.
So what about hooks? My first impression was skip them completely, but I think we could provide a single hook, which would trigger re-render. This hook would be used only once in the root of your app like this:
Everywhere else you would be fine with macros:
Macros
Transforming macros into
i18n._
calls is just one thing. The main purpose is to transform JSX or tagged template literals into ICU Message Format. That includes plurals and date/number formatting:The same API can be used also in projects without React.
However in JSX it’s more convenient to use components instead, which allows us to use inline components without any limitations:
Conclusion
That’s why I don’t want to use hooks for translation:
On the other hand, I’m open to
useLingui
hook which returns active locale that could trigger re-render on locale change.What’s your opinion about all of this?
It is, actually! If you use it outside component, then the translation is evaluated only once when the module is loaded. Most probably the translations won’t be loaded at the time.
This is a problem and we still need to figure out how to handle it. More common usecase are translations in default props:
It’s the same problem. When module is loaded,
Image caption
is translated, but at that point we might not know the active locale or message catalogs aren’t loaded. Usually this is solved using lazy translations, i.e.: you define that this string should be translated, but it isn’t translated immediately: