React.js/Redux: setInterval and clearInterval in Reducers React.js/Redux: setInterval and clearInterval in Reducers reactjs reactjs

React.js/Redux: setInterval and clearInterval in Reducers


Don't do this in the reducer. Reducers should be pure functions (meaning no side-effects). Instead, you could create a component that is rendered only when the game is running (a boolean state which is set to true by the START action and false by the STOP action). Then in the component's componentDidMount function call setInterval with a function that dispatches a NEXT_GENERATION action. After that dispatch another action which adds the timer id to the store. Then, in the componentWillUnmount call clearInterval with the timer id.