I used to call AJAX after props change by ComponentWillReceiveProps()
componentWillReceiveProps(nextProps) {
if(nextProps.foo !== this.props.foo){
//fetch & call this.setState() asynchronously
}
}
After React 16.3 ComponentWillReceiveProps()
is going to be deprecated in the future. Instead of ComponentWillReceiveProps()
there is a new function getDerivedStateFromProps
, but I can't update state async.
static getDerivedStateFromProps(nextProps, prevState) {
// I can't get access to this.props but only state
if(nextProps.foo !== this.props.foo){
//fetch & call this.setState() asynchronously
}
// I can only return a state but update it after an AJAX request
return {}
}
What's the best practice to do it.