ParaglideJS: Recommended pattern for reactive UI change in React? #2487
Unanswered
rsimon
asked this question in
[inlang SDK] Feature Requests
Replies: 0 comments 4 replies
-
The workaround seems good. Optimizing re-renders for language changes is the wrong direction. A user changes the language once. That one change can trigger a re-render of the entire app, which is fine. @rsimon are you using paraglide JS with only React (not NextJS)? @LorisSigrist agree that this workaround is good? |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
I'm struggling to find a good pattern for re-rendering a React UI based on a language change. I wondered if anyone has thoughts on a recommended pattern, since I guess it must be a common requirement?
My use case is simple: I have a language switcher dropdown, and if the user changes the language, the whole UI should re-render in the new language.
I believe I saw some people use the current language as a
key
property to their top-level component. At least in React, however, changing thekey
will unmount the whole component tree and re-mount new components, thus resetting all component state. Are there any better solutions?My current workaround is a React Context that holds the current language as a state, and I'm updating it through
onSetLanguageTag
. I also created a custom hookuseLanguage()
(which returns the current language tag).Once I use that hook inside a component, the changing state will force a re-render. That solves my problem. But it still feels odd, because I'm not actually doing anything with the hook return value. I just add the hook at the top of my component.
As I said: works. But doesn't really feel like the best solution. If anyone has thoughts or recommendations, I'd love to hear them!
Beta Was this translation helpful? Give feedback.
All reactions