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.
You should not use getDerivedStateFromProps
lifecycle for making api calls. Instead, use componentDidUpdate
to make api call and once you get the api response do this.setState
. Also as pointed in another answer, you cannot use this
in static method.
componentDidUpdate(prevProps) {
if (this.props.myData !== prevProps.myData) {
this.callMyApi();
}
}
callMyApi() {
fetch("/api")
.then(response => {
this.setState({ ... });
})
}
If you are writing new component, you can also consider to write a Functional component and use useState
and useEffect
to trigger api call when a propis updated.
Like this:
...
const {page} = this.props;
const [images, setImages] = useState([]);
useEffect(() => {
fetch(`/myfavApi`)
.then(data => data.json())
.then(images => {
setImages(images.concat(images));
});
}, [page]); // provide page(prop) as dependency.
...