Parent react components stay unchanged when using I18n.changeLanguage(lng)
See original GitHub issueDescribe the bug It in some way similar to #611 May be a bug, but may be i’m doing something wrong.
Occurs in react-i18next version 10.11.4
To Reproduce
I create language switcher component. Use it in root component.
In all other components I use Trans
and useTranslation
.
After language switch only swticher component is updated.
But, if I use changeLanguage
directly in root component, app will be rerendered as expected.
Should I use I18nextProvider
or something?
import React, {useCallback} from 'react';
import {useTranslation} from 'react-i18next';
const LanguageSwitcher = ({className}) => {
const {i18n} = useTranslation();
const setEnglish = useCallback(() => {
i18n.changeLanguage('en');
}, [i18n]);
const setRussian = useCallback(() => {
i18n.changeLanguage('ru');
}, [i18n]);
return (
<div className={className}>
<button
onClick={setEnglish}
>
English
</button>
<button
onClick={setRussian}
>
Русский
</button>
</div>
);
};
export default LanguageSwitcher;
Expected behaviour
All components, that use useTranslation
should recive new t
and/or i18n
and rerender when language switched.
OS (please complete the following information):
- Device: Ubuntu 18.04
- Browser: Yandex.Browser 19.4.2.698 beta (64-bit)
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
i18next/i18n Change Language not working with the whole ...
I can't seem to get the whole site running on a different language. My index.tsx file import React from 'react'; import ReactDOM from...
Read more >useTranslation() Hook - React Deep Dive - Educative.io
However, using the useTranslation() Hook often simplifies the component and makes it ... const changeLanguage = (lng) => {. 8. i18n.changeLanguage(lng);.
Read more >[Solved]-Detect browser's language with i18next-browser ...
lng =es as well as when using a browser extension that let me change the browser language. Heres my working i18n.ts file: import...
Read more >How to properly internationalize a ... - DEV Community
'bold' : 'normal' }} type="submit" onClick={() => i18n.changeLanguage(lng)}> {lngs[lng] ...
Read more >youtube-danmaku - Codul sursă - Greasy Fork
{ // By explicitly using `prop-types` you are opting into new production ... the creation of the component, // children are handled after...
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 Free
Top 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
downside -> when using multiple namespaces
t
can be bound to a namespaceit gets a
t
function -> it just does not get any update on languageChanged…that’s “all” (beside having a t function bound to global configured defaultNamespace)The
Trans
component does not get a magical rerender on languageChange -> only hook, HOC get that, so correct would be passingt
to the Trans component (Trans component does only enable translation of JSX nodes - nothing more):