React introduced new static method getDerivedStateFromProps(props, state)
which is called before every render method, but why? Calling it after prop change makes se
I did something like this
constructor(props) {
super(props);
this.state = {
expanded: props.expanded,
ownUpdate: false
}
}
static getDerivedStateFromProps(props, state) {
if (state.ownUpdate) {
return {
expanded: state.expanded,
ownUpdate: false
};
} else if (props.expanded !== state.expanded) {
return {
expanded: props.expanded
};
}
return null;
}
toggle() {
this.props.onAftePress(this.state.expanded, this.props.index);
this.setState({
expanded: !this.state.expanded,
ownUpdate: true
})
}