"Cannot update a component" when calling changeLanguage()
See original GitHub issueI 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:
- Created 3 years ago
- Comments:5 (4 by maintainers)
Top 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 >
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
Known to me 😅. This is due to the fact that
i18next.changeLanguage
is async. This lineShould be fixed in the latest version