erikras / react-redux-universal-hot-example

A starter boilerplate for a universal webapp using express, react, redux, webpack, and react-transform
MIT License
12.01k stars 2.5k forks source link

onEnter called multiple times #1234

Open rbhalla opened 8 years ago

rbhalla commented 8 years ago

EDIT: A much simpler way to reproduce

<IndexRoute component={Home} onEnter={() => console.log('Home page')}/>

Shows Home page 3 times. Twice on the client console, and once on the server side.

I'm keeping the below just for reference, otherwise the above is enough to replicate the problem.


I've seen some references to this issue elsewhere, but the fixes outlined (using react-router-redux, check for multiple dispatches) seem to be taken care of.

I've added this to my routes file:

<Route path="/verify-email/:token" onEnter={verifyEmailAndRedirect(store)} component={Email} />

And this is the onEnter function

import {verifyEmail} from 'redux/modules/user';
import {createFlashMessage} from 'redux/modules/flash';

export function verifyEmailAndRedirect({dispatch}) {
  return ({params: {token}}) => {
    dispatch(verifyEmail(token))
      .then(
        // Success
        ({message}) => {
          dispatch(createFlashMessage(dispatch, message, 'info', true));
        },
        // Failure
        message => {
          dispatch(createFlashMessage(dispatch, message, 'danger', true));
        }
      );
  };
}

The URLs associated with this route are never linked to in the app, so a fresh app load will always occur. Therefore, whenever I test this, I refresh the page with a URL that will trigger this route. What I've noticed however, is 3 flash messages are always created. If I place a console.log call just before the verifyEmail dispatch, I get 2 calls on the client, and 1 on the server.

If I use react-router's async done callback, I get 2 rendering, and a warning in my console telling me that the server render and the client render are different.

Anyone have any idea where I might start troubleshooting this issue? I'd rather not hack around the problem with debounces.

wezleytsai commented 7 years ago

this fixed it for me https://github.com/ReactTraining/react-router/issues/3016