react-route's onEnter handler called twice
I've found, that applying current react-router-redux (4.0.7) causes twice calling react-router's asynchronous (with 3 arguments) onEnter handler while initial loading. Here is a gist, describing this issue https://gist.github.com/semurgx/9abb0d02ea49f49aa97ef34cac117f5f .
I'm seeing this as well since upgrading this week, though in a different scenario, not on the initial page render but at other times in the application after a push. Also, for me I only see it with hash history (I see you are using browser history). Also, using the example from your gist, if you don't put a setTimeout in there then it only gets called once. It's interesting that even a setTimeout value of zero causes it to be called twice.
const handleEnter = (nextState, replace, cb) => {
console.log('whoa! called twice');
//setTimeout(cb, 500); <-- will cause onEnter to be called twice
//setTimeout(cb, 0); <-- will cause onEnter to be called twice
cb(); // <-- Will cause onEnter to be called only once
};
I recently upgraded a number of packages along with this one and can't figure out what is causing this. With react-router-redux 4.0.7 comes react-router v.3 and history v.3 but even when I fall back to previous versions I can't get it back to how it used to be, which makes me think I'm missing something else here, maybe another supporting package or something else I blindly upgraded and can't identify.
In the meantime I am planning to use a debouce on my onEnter function...
Experiencing exactly the same issue. Also working around using debounce for now.
Delete this line can fix this issue. https://github.com/reactjs/react-router-redux/blob/3dfbddef27cbe4435ed96e63fcc05dc63081e001/src/sync.js#L130
Another way to fix this issue:
wrap https://github.com/reactjs/react-router-redux/blob/3dfbddef27cbe4435ed96e63fcc05dc63081e001/src/sync.js#L102 line with isTimeTravelling = true and isTimeTravelling = false.
I did also had this issue and I fixed it by downgrading react-router to 2.x
I stumbled upon this issue as well. Seems like a serious one to me. A downgrade to 4.0.6 fixed it for the time being.
Encountered the same issue and I implemented a workaround with shouldComponentUpdate for this:
export default class RootRoute extends Component {
render() {
// Create an enhanced history that syncs navigation events with the store
const history = syncHistoryWithStore(browserHistory, store)
return (
<Router history={history}>
<Route component={App}>
<Route path="/foo" component={Foo} />
<Route path="/bar" component={Bar} />
</Route>
</Router>
);
}
}
export default class App extends Component {
shouldComponentUpdate(nextProps) {
// performance workaround until react-router-redux fixes https://github.com/reactjs/react-router-redux/issues/481
return nextProps.location.action === 'POP';
}
render() {
return (
<h1>Hello Word!</h1>
);
}
}
Oh man, I was pulling my hair out because my injected sagas (in onEnter) were running twice and I thought I had messed up somewhere. Reverted to 4.0.6 and the problem went away.
Just to get some more information: What version of react-router and history is everyone using?
Latest stable.
Technically, history 4.5.1 is the latest stable version, but we don't support that. I need your exact version numbers to be helpful.
OK, I have an example set up on this project: https://github.com/michaelBenin/react-ssr-spa. On the master branch, upgrade react-redux to 4.0.7.
You're using history 4.4.1 with react-router 3.0.0, but we only support history 3.x.x. You have fundamentally incompatible versions installed.
I've created a branch that has the correct versions:
https://github.com/michaelBenin/react-ssr-spa/compare/develop-react-router-redux-repro
Interesting...
No explicit dependency from 'history' in my project, and react-router v3.0.0 with react-router-redux v4.0.7 share the same lib version (history v3.2.1 if it will be helpful). But still have same issue - onEnter called twice for some reasons..
As I can understand it's rather related to react-router itself, see reference above.
It was closed when it was a question, but they think about reopening it as a bug.
Also, something else I found to be very strange.
In src/sync, when calling:
history.getCurrentLocation()
This yields different results from client and server, hence invariant errors upgrading from 4.0.6 to 4.0.7.
On server:
{ pathname: '/repo/michaelBenin/react-ssr-spa',
search: '',
hash: '',
state: undefined,
action: 'POP',
key: null,
query: {} }
Where as in browser, key has a value of a string and not null.
I currently have not been able to upgrade my app from 4.0.6, should we open a different issue for this on history? calling history.getCurrentLocation on server yields different values?
@michaelBenin That initial null key is described in the 3.0 changelog: https://github.com/mjackson/history/blob/v3.x/CHANGES.md#300-0
Ok good to know that is expected in the history library.
Should this be a different issue for react-router-redux that 4.0.7 is breaking server rendering causing an invariant error because history.getCurrentLocation is giving different results in initial render in client and server?
Would you be open to reviewing a pr at attempted to fix this key having a different value?
Thank you.
BTW, I put the original gist into a jsfiddle here: https://jsfiddle.net/n4ovmzzf/ Just click Run at the top after it loads because the result iframe URL changes from the first load and the route won't match anything initially.