question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't detect i18n.language changes in componentDidUpdate or shouldComponentUpdate

See original GitHub issue

Describe 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
jamuhlcommented, Apr 18, 2019

Do you need the previous language?

if not:

  componentDidUpdate(prevProps, prevState) {
    if (this.props.t !== prevProps.t) {
      console.log(this.props.i18n.language);
    }
  }

if yes:

  • you need to persist lng on state to check for old

=> the if (this.props.i18n !== prevProps.i18n) won’t ever trigger - the i18n instance stays the same

1reaction
jamuhlcommented, Apr 18, 2019

So 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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found