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.

"Cannot update a component" when calling changeLanguage()

See original GitHub issue

I have a very basic <LanguageSwitch /> component that looks like this

import React from 'react';
import { useI18next } from 'gatsby-plugin-react-i18next';

export function LanguageSwitch() {
  const { languages, language, changeLanguage } = useI18next();
  return (
    <select
      onChange={(event) => changeLanguage(event.target.value)}
      value={language}
    >
      {languages.map((lang) => (
        <option key={lang} value={lang}>
          {lang}
        </option>
      ))}
    </select>
  );
}

When I now change the language on my website I get following message in my browser console:

index.js:2177 Warning: Cannot update a component (`LanguageSwitch`) while rendering a different component (`PageRenderer`). To locate the bad setState() call inside `PageRenderer`, follow the stack trace as described in https://fb.me/setstate-in-render
    in PageRenderer (at query-result-store.js:86)
    in PageQueryStore (at root.js:56)
    in RouteHandler (at root.js:78)
    in div (created by FocusHandlerImpl)
    in FocusHandlerImpl (created by Context.Consumer)
    in FocusHandler (created by RouterImpl)
    in RouterImpl (created by Context.Consumer)
    in Location (created by Context.Consumer)
    in Router (created by EnsureResources)
    in ScrollContext (at root.js:69)
    in RouteUpdates (at root.js:68)
    in EnsureResources (at root.js:66)
    in LocationHandler (at root.js:124)
    in LocationProvider (created by Context.Consumer)
    in Location (at root.js:123)
    in Root (at root.js:132)
    in StaticQueryStore (at root.js:138)
    in _default (at app.js:94)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jimmyncommented, May 29, 2020

Known to me 😅. This is due to the fact that i18next.changeLanguage is async. This line

0reactions
jimmyncommented, Feb 23, 2021

Should be fixed in the latest version

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot update a component (`withI18nextTranslation ... - GitHub
This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested ...
Read more >
useEffect can't update the state - Stack Overflow
I have a part of code that loads the stored language of i18n. I am trying to get the language, change the state...
Read more >
Step by step guide (v9) - react-i18next documentation
Call i18n.changeLanguage is all needed to do. import React from 'react';. import i18n from './i18n';. import { withNamespaces } from 'react-i18next';.
Read more >
cannot update a component | The AI Search Engine You Control
To fix using useEffect() : In knob.js , you can store panLevel as state first just like in App, instead of direct calling...
Read more >
Can not update a component while updating another ... - Reddit
Looking into this error, my understanding of the underlying problem is that ErrorModal is changing the state at the wrong time, but there...
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