Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite recursion when dispatching action in onEnter hook. #70

Open
Furizaa opened this issue Sep 21, 2015 · 9 comments
Open

Infinite recursion when dispatching action in onEnter hook. #70

Furizaa opened this issue Sep 21, 2015 · 9 comments
Labels

Comments

@Furizaa
Copy link

Furizaa commented Sep 21, 2015

To initially fill the state of my app on the server, components subscribe to the onEnter hook of their routes. In this static initialisation functions I dispatch some actions.

Now this code in redux-router kicks in:

https://github.com/rackt/redux-router/blob/master/src/client.js#L29

... and replaces the history again (calling onEnter again) ...

https://github.com/rackt/redux-router/blob/master/src/client.js#L37

... because it didn't had the chance to set routerState which initially is set to undefined ...

https://github.com/rackt/redux-router/blob/master/src/client.js#L14

... resulting in an infinite loop.

Just setting routerState before the replaceHistory call fixes the loop - but everything still gets called twice.

store.subscribe(() => {
  const nextRouterState = routerStateSelector(store.getState());

  if (
    nextRouterState &&
    !routerStateEquals(routerState, nextRouterState)
  ) {
    const { state, pathname, query } = nextRouterState.location;
    routerState = nextRouterState;
    history.replaceState(state, pathname, query);
  } else {
    routerState = nextRouterState; 
  }
});

There has to be a way to initialise routerState with the correct state instead of undefined on the first call.

@thom-nic
Copy link

I'm seeing this issue, I originally commended on remix-run/react-router#1977 thinking it was in react-router but it actually seems it's specific to redux-router. Here's a screenshot of what I see in my console:

JS console errors on HMR

Those XHRs in the log are called from action dispatches that are triggered from an onEnter defined in my route.

@Scarysize Scarysize added the bug label Dec 24, 2015
@Scarysize
Copy link
Contributor

@Furizaa @thom-nic
will reopen if this is still an issue.

@phun-ky
Copy link

phun-ky commented Jan 27, 2016

I'm getting a similar issue with store.subscribe which makes it execute twice on a single pushState/replaceState.. We're using v1.0.0-beta6 with hot-middleware/connect

@thom-nic
Copy link

I moved away from use of onEnter to componentWillMount & componentWillUpdate in my page components which circumvented this issue. I don't know if onEnter still has an issue or not.

@ghost
Copy link

ghost commented Apr 20, 2016

I still get this onEnter issue.

@Scarysize Scarysize reopened this Apr 20, 2016
@Scarysize
Copy link
Contributor

@srahulprdxn would mind providing us with a gist?

@ghost
Copy link

ghost commented Apr 25, 2016

Yea sure. I'll post it soon.

@ghost
Copy link

ghost commented Apr 27, 2016

@Scarysize I am almost writing the same code as @Furizaa . So you can consider his comment for reference. I cannot provide you with my gist as I have moved with another solution which is just a work around.

@dentuzhik
Copy link

I get infinite loop when trying to fire an action from history.listenBefore, and strangely enough it occurs only during HMR process.
Not sure if this is the same problem, or different though. But seems it's the same, as described here: reactjs/react-router-redux#30

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants