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.

Bug: Cannot update a component (`ConnectFunction`) while rendering a different component (`Unknown`) - console warning for function components without children

See original GitHub issue

Warning in console logs: Cannot update a component ('ConnectFunction') while rendering a different component ('Unknown') It was described here and it is quite clear and very valid warning indicating not recommended design pattern. However, I stumbled across a different scenario similar to the described problem where the function component causing the warning does not have any children. In my case, the scenario is the component inside the <Router> of react-router-dom which observes some particular pathname and search params and triggers the redux action dispatcher.

image

React version: 16.8.6

Steps To Reproduce

  1. use React-router-dom
  2. create simple function component to observe react-router props and place it as a child of <Router>
  3. use it with conjuction of withRouter from react-router-dom and connect from ‘react-redux’
  4. call action creator form inside the function component
const ObservePath = (props: Props) => {
  const { location } = props;
  if (!location) {
    return null;
  }
  props.savePathname({
    location.pathname,
  });
  return null;
};

export default withRouter(connect(undefined, {
  savePathname,
})(ObservePath));

// usage

<Router>
  <ObservePath />
  ...

The current behavior

long warning in the console

The expected behavior

no warning in this case

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
webduvetcommented, Jul 4, 2020

I should have pointed out that the component does not render anything it has return null, but that does not change anything as ‘rendering’ means executing the function of the function component regardless of the return. Thanks, for the explanation.

0reactions
bvaughncommented, Jul 4, 2020

This warning doesn’t have anything to do with whether or not a component has children. The warning is whether one component tries to update state in a different component while rendering.

We recommend not doing that, so we warn about it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

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 >
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 ... - You.com
The error message will be displayed if parent (App) states are set while rendering children (Knob). In your case, while App is rendering,...
Read more >
Cannot update a component while rendering a different ...
Let's see what caused this warning and how to fix it. The following code samples are not from the actual app, they are...
Read more >
Cannot update a component (`StoreProvider`) while rendering ...
Error: Cannot update a component (`StoreProvider`) while rendering a different component (`ProductDescription`). To locate the bad setState() ...
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