一,报错
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`.
二,原因
在渲染函数 render 输出的时候,修改了 props 或者 state 的值,由于 render 输入依赖 props 和 state 的值,如果在渲染时这些值被修改,就会导致以上报错
三,方法
1,将 render 函数里,可能会触发修改 props 或者 state 的代码,移到 componentWillMount
2,如果是组件加载后,重复渲染 render 时需要修改 props 和 state,则利用 shouldComponentUpdate 函数提前处理
Leave a Reply