Los Morales Los Morales - 1 year ago 241
React JSX Question

React-router producing setState warning

I'm using React, Redux and React-Router. In my MapDispatchToProps() in a React component, I check for a condition and if that condition is true, I call router's browserHistory.push("/newLocation"). For some reason I get this error:

Warning: setState(...): Cannot update during an existing state
transition (such as within
or another component's
constructor). Render methods should be a pure function of props and
state; constructor side-effects are an anti-pattern, but can be moved

The funny thing is that my React components are stateless (pure JS functions) so I know I'm not explicitly setting state in a render method. Diving into the error looks like it is react-router that is trying to set the state:


Although even with this warning, the application seems to behave ok. Not sure how to get rid of this warning though.

Answer Source

The push functionality is setting state. You should use the onEnter functionality built into react-router to test for a condition before entering a route

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download