一. 问题描述
一个页面,根据从不同的页面跳转过来的,显示 提示条,并实现其他不同的参数配置。结果代码中的setstate 会报错:
Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.
报错信息的意思是,setState写到那个代码直接写在组件中,而不是组件的某个方法中,会导致 一直加载调用。
二. 问题解决
参考文档:https://itnext.io/react-setstate-usage-and-gotchas-ac10b4e03d60
所以在代码中加入 componentWillReceiveProps() 方法, 就执行成功了。之前还试过 componentDidMount、componentWillMount、componentWillUpdate等方法,均失败。
即代码如下:
interface Estate {
show3?: boolean;
}
class EnList extends React.Component<Props, Est