I have a component where I set initial state for it. When I change, in my case, checkbox state, the parent component should send new property with flag 'true' or 'false' and it does if I console.log it in my render method. However, if I use lifecycle method static getDerivedStateFromProps - it shows me only updated values from previous state and new props (like 'true', 'true' instead of previous 'false', and new 'true'). The question is: Can't figure out, where is the pitfalls here? It sends new props if I console.log it in my render method, but getDerivedStateFromProps shows the same values both in prevState and newProps.
class MyComponent extends React.PureComponent {
state = {
stateValue: this.props.myValue
}
handleChange = () => {
// Custom parent method which change checkbox state and send new props to MyComponent
this.props.onChange(...)
}
static getDerivedStateFromProps(nextProps, prevState) {
const { myNextProperty } = nextProps
const { myPrevStateProperpty } = prevState
debugger
if (myNextProperty !== myPrevStateProperpty) {
return {
stateValue: nextProps,
}
}
return null
}
render() {
const { stateValue } = this.state
return (
<MyViewComponent onChange={this.handleChange} value={stateValue} />
)
}
}