Bug: Cannot update a component (`ConnectFunction`) while rendering a different component (`Unknown`) - console warning for function components without children
See original GitHub issueWarning 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.
React version: 16.8.6
Steps To Reproduce
- use React-router-dom
- create simple function component to observe react-router props and place it as a child of <Router>
- use it with conjuction of
withRouter
from react-router-dom andconnect
from ‘react-redux’ - 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:
- Created 3 years ago
- Comments:5
Top 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 >
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
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.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.