Currently I am setting ownUpdate: true
state when we are changing state from same component and check it in getDerivedStateFromProps accordingly. Below is the code I am using in my component. Can anyone suggest better way of doing this. Thanks in advance.
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. onAfterPress(this.state.expanded, this.props.index);
this.setState({
expanded: !this.state.expanded,
ownUpdate: true
})
}