I am trying to reset React state variables (to default values) in a container using setState() method. But getting the following 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`.
And finally: Maximum call stack size exceeded.
My code below:
resetMsg=()=> {
const company = this.state.company;
company.id = 0;
company.messages = [];
this.setState({company: company});
}
I am calling resetMsg() when variable in Redux state is true.
Code where I call resetMsg (The value of resetMessages is false initially and I need to reset React-state, when its true ):
render() {
if(this.props.resetMessages){
this.resetMsg();
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…