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.

Infinite re-mounting when dispatching in componentDidMount of a route component

See original GitHub issue

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

After upgrading to latest connected-react-router and react-redux. When I mount a routed component that does a dispatch inside componentDidMount it gets into an infinite loop until eventually it hits the Maximum update depth exceeded error.

This only seems to happen inside a component that’s mounted through a route. I use the component prop on Route for all components.

What is the expected behavior?

It’s a simple action. And the action seems to be performed, so it’s expected to not re-mount in an infinite loop which is what I believe is going on. The same redux action works on any other component that is a parent of that Switch.

It also works fine with the standard Router from react-router-dom.

Which versions and which browser and OS are affected by this issue?

This is with @latest and @next of all 3:

"connected-react-router": "^6.0.0-beta.1",
"react-router-dom": "^4.4.0-beta.6",
"react-redux": "^6.0.0-beta.3",

or

"connected-react-router": "^6.0.0",
"react-router-dom": "^4.3.1",
"react-redux": "^6.0.0",

I have tried with older versions of react-router and older and latest versions of redux, same issue.

Possible reason

This seems to only happen on a Routed component that is inside my second nested Switch. So my current app structure looks like:

App.js

<Provider store={store}>
     <ConnectedRouter history={history}>
            <Switch>
                   <Route path="/login" key="/login" exact component={Login} />
                   <Route component={authRoute(RoutedLayout)} locale={this.state.locale} />
            </Switch>
     </ConnectedRouter>
</Provider>

And RoutedLayout.js:

<div>
    ...
    <Layout>
    <SideMenu />
    ... etc.
         <Content>
              <Switch>
                    {Routes().routes.map((route) => (
                             <Route
                                   path={route.path}
                                   key={route.path}
                                   exact
                                   component={componentFactory(route.component)}
                               />
                     ))}
                     <Route
                              path="/403"
                              key="/403"
                              exact
                              component={PermissionDenied}
                       />
                       <Route component={authRoute(NotFound)} />
                 </Switch>
           </Content>
     </Layout>
</div>

So any Route component that is rendered in the RoutedLayout Switch will cause this issue if it dispatches from componentDidMount.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:8
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
allimuucommented, Dec 12, 2018

This is what the console looks like and the error when it eventually hits the max, the component actually mounts just fine after that:

infinite remount

1reaction
simontongcommented, Jan 2, 2019

I’m still getting the issue.

Forward button does not seem to work anymore when I hit back.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React-redux infinite remounting component - Stack Overflow
I'm beginner in react/redux and I came across a weird behaviour of my application every try of dispatching action e.g. store.dispatch({type: ' ...
Read more >
DOM isn't ready in time after componentDidMount in React ...
componentDidMount is only called once in the lifecycle of any component, render againt will not reinitialize the component.
Read more >
How to solve too many re-renders error in ReactJS?
After mounting a React component, it will listen to any React props or state that has changed. It will, by default, re-render the...
Read more >
Rendering and Updating Data using Component Lifecycle ...
There are a few methods used in the mounting phase: constructor(). render(). componentDidMount().
Read more >
componentdidmount react hook Code Example - Code Grepper
Similar to componentDidMount and componentDidUpdate: 6. useEffect(() => {. 7. // Update the document title using the browser API.
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