Los Morales
Los Morales

Reputation: 2155

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 render 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 to componentWillMount.

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:

Stracktrace

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

Upvotes: 1

Views: 930

Answers (1)

Sina Khelil
Sina Khelil

Reputation: 1991

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

Upvotes: 1

Related Questions