Search code examples
reactjsreduxreact-reduxredux-actions

When i dispatch action to redux from react in line 1 can i be 100% sure that line 2 in my react component has the updated redux state?


I know Dispatching actions in redux is sync. When i dispatch action to redux from react component in line 1 can i be 100% sure that line 2 in my react component has the updated redux state? I'm setting game data by dispatching getGameDataSuccess(data) action. Can i be 100% sure that in the immediate next line the new props will flow down? Right now when i console.log(this.props.lostLetters.gameData), i see the new game data. But can i be 100% sure this will always be the case?

getLostLettersGame(this.props.gameCenter.selectedGame.gameId)
              .then((data) => {
                this.props.dispatch(LostLettersActions.getGameDataSuccess(data));
                console.log(this.props.lostLetters.gameData);
                this.generateGame();
              })

Solution

  • You cannot be 100% sure that the updated state is rendered right after the dispatch call. mapStatetoProps is called when the component is about to re-render, which depends on whether React batches the updates or not. By default, React batches updates from event handlers.

    You can refer https://github.com/reactjs/react-redux/issues/291