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 while rendering a different component" warning caused by `useSelector`

See original GitHub issue

using 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:

  1. a parent component pulls from some redux slice with useSelector
  2. 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)
  3. 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:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
markeriksoncommented, Oct 9, 2020

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 a useEffect, directly in the body of the render logic. Changing that code to not dispatch while rendering should fix the issue.

1reaction
markeriksoncommented, Oct 3, 2021

@Bharateshwar as per above: the issue is not useSelector - the issue is triggering state updates while rendering, such as dispatching a Redux action.

Read more comments on GitHub >

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

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