"Cannot update a component while rendering a different component" warning caused by `useSelector`
See original GitHub issueusing react 16.13.1 and react-redux 7.2.1 I get this new “cannot update a component while rendering a different component” react warning in the following scenario:
- a parent component pulls from some redux slice with
useSelector
- a child of that parent also pulls from the same redux slice with
useSelector
(can be the same or a different value of that slice) - the parent dispatches an action that updates that redux slice
=> 💥 I get the warning on the console “Warning: Cannot update a component (SomeChild) while rendering a different component (SomeParent). To locate the bad setState() call inside SomeParent, follow the stack trace…”
if my implementation is an antipattern, what’s the expected pattern? that parents should be aware if any of their children want to access the same redux slice as them, and if so pull the value for them and drill it down to the appropriate child?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:4
- Comments:8 (4 by maintainers)
Top Results From Across the Web
How to fix the "cannot update a component while rendering a ...
While working on a React / Next.js application I got this error: Cannot update a component (`App`) while rendering a different component.
Read more >Cannot update a component while rendering a different ...
This warning was introduced since React V16.3.0. If you are using functional components you could wrap the setState call into useEffect.
Read more >cannot update a component while rendering a ... - You.com
In your case, while App is rendering, Knob's onChange() is triggered when loaded, which then calls this.handleOnChange() and then this.props.handleChangePan() ...
Read more >JavaScript : Cannot update a component while rendering a ...
JavaScript : Cannot update a component while rendering a different component warning [ Gift : Animated Search Engine ...
Read more >Cannot update a component while rendering a different ...
One of the most common reasons that your components might not be re-rendering is that you're modifying the existing state in your reducer...
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 FreeTop 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
Top GitHub Comments
The issue only occurs if you are actually queuing state updates while rendering, which is not allowed in React in general.
For background on the specific React behavior here, see https://github.com/facebook/react/issues/18178 .
I’d recommend looking at
SomeChild
’s render logic, and find a place where you’re dispatching an action outside of a click handler or auseEffect
, directly in the body of the render logic. Changing that code to not dispatch while rendering should fix the issue.@Bharateshwar as per above: the issue is not
useSelector
- the issue is triggering state updates while rendering, such as dispatching a Redux action.