Can't detect i18n.language changes in componentDidUpdate or shouldComponentUpdate
See original GitHub issueDescribe the bug
There is a function that i need to trigger based on the language changes, but some how I can’t detect the changes in componentDidUpdate
or shouldComponentUpdate
. And I tried to console log this.props.i18n.language
and prevProps.i18n.language
, they are just the same
Occurs in react-i18next version v.10.7.0
To Reproduce
Here is a codesandbox example https://codesandbox.io/s/62v094rj6z.
You can look at the Homepage.js
This one is a simplified version, but I think it can still demonstrate the idea that it can’t detect the props changes in lifecycle event
Expected behaviour
In the codesandbox example, the Homepage
component should update normally when i click on the links.
OS (please complete the following information):
- Device: [e.g. MBP 2017 13"]
- Browser: [e.g. Chrome 73.0.3683.103]
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:9 (5 by maintainers)
Top Results From Across the Web
i18next React - Changing language only works after browser ...
In react, try to use the useTranslation hook or the withTranslation HOC. That will detect the language change.
Read more >“should component update react only certain states” Code ...
Answers related to “should component update react only certain states”. can't perform a react state update · use state value change right after...
Read more >How to use componentDidUpdate in React - DEV Community ...
componentDidUpdate () takes as its first two arguments the previous props and the previous state. Inside the method we can check if a...
Read more >How to properly internationalize a React application using ...
detect user language // learn more: https://github.com/i18next/i18next-browser-languageDetector .use(LanguageDetector) // pass the i18n instance to ...
Read more >React Native Interview Preparation | by Ravi Sharma - Medium
A component should not change its own props, only react to changes of props from the parent component. It is immutable and can't...
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
Do you need the previous language?
if not:
if yes:
=> the
if (this.props.i18n !== prevProps.i18n)
won’t ever trigger - the i18n instance stays the sameSo you do not really need the previous language - and the hook, HOC whatever will trigger a rerender anyway…also you could listen for
i18next.on('languageChanged', () => {})
or do i miss something?