react-router-redux icon indicating copy to clipboard operation
react-router-redux copied to clipboard

react-route's onEnter handler called twice

Open semurgx opened this issue 10 years ago • 38 comments

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 .

semurgx avatar Nov 08 '16 12:11 semurgx

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...

wphampton avatar Nov 09 '16 14:11 wphampton

Experiencing exactly the same issue. Also working around using debounce for now.

hilkeheremans avatar Nov 17 '16 09:11 hilkeheremans

Delete this line can fix this issue. https://github.com/reactjs/react-router-redux/blob/3dfbddef27cbe4435ed96e63fcc05dc63081e001/src/sync.js#L130

mofelee avatar Nov 17 '16 11:11 mofelee

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.

semurgx avatar Nov 17 '16 14:11 semurgx

I did also had this issue and I fixed it by downgrading react-router to 2.x

gerhardsletten avatar Dec 05 '16 22:12 gerhardsletten

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.

amarinov avatar Dec 21 '16 07:12 amarinov

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>
    );
  }
}

tvanro avatar Dec 22 '16 07:12 tvanro

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.

neverfox avatar Jan 13 '17 00:01 neverfox

Just to get some more information: What version of react-router and history is everyone using?

timdorr avatar Jan 13 '17 15:01 timdorr

Latest stable.

michaelBenin avatar Jan 13 '17 21:01 michaelBenin

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.

timdorr avatar Jan 13 '17 21:01 timdorr

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.

michaelBenin avatar Jan 13 '17 21:01 michaelBenin

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.

timdorr avatar Jan 13 '17 22:01 timdorr

I've created a branch that has the correct versions:

https://github.com/michaelBenin/react-ssr-spa/compare/develop-react-router-redux-repro

michaelBenin avatar Jan 13 '17 22:01 michaelBenin

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.

m-shulipa avatar Jan 13 '17 22:01 m-shulipa

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.

michaelBenin avatar Jan 13 '17 22:01 michaelBenin

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 avatar Jan 13 '17 22:01 michaelBenin

@michaelBenin That initial null key is described in the 3.0 changelog: https://github.com/mjackson/history/blob/v3.x/CHANGES.md#300-0

timdorr avatar Jan 16 '17 14:01 timdorr

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.

michaelBenin avatar Jan 16 '17 16:01 michaelBenin

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.

timdorr avatar Feb 08 '17 18:02 timdorr