React-router 产生 setState 警告
React-router producing setState warning
我正在使用 React、Redux 和 React-Router。在我的 React 组件中的 MapDispatchToProps() 中,我检查一个条件,如果该条件为真,我调用路由器的 browserHistory.push("/newLocation")。出于某种原因,我收到此错误:
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
.
有趣的是我的 React 组件是无状态的(纯 JS 函数)所以我知道我没有在渲染方法中明确设置状态。深入研究错误看起来是 react-router 试图设置状态:
虽然即使有这个警告,应用程序似乎也能正常运行。不过不确定如何消除此警告。
推送功能正在设置状态。您应该使用 onEnter
内置于 react-router 中的功能在进入路由之前测试条件
我正在使用 React、Redux 和 React-Router。在我的 React 组件中的 MapDispatchToProps() 中,我检查一个条件,如果该条件为真,我调用路由器的 browserHistory.push("/newLocation")。出于某种原因,我收到此错误:
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 tocomponentWillMount
.
有趣的是我的 React 组件是无状态的(纯 JS 函数)所以我知道我没有在渲染方法中明确设置状态。深入研究错误看起来是 react-router 试图设置状态:
虽然即使有这个警告,应用程序似乎也能正常运行。不过不确定如何消除此警告。
推送功能正在设置状态。您应该使用 onEnter
内置于 react-router 中的功能在进入路由之前测试条件