AJAX request should be done at the componentDidMount
hook when you need to load data inmediatelly after the component is first rendered. When you wish to refresh the data after some props changed, you must use the componentDidUpdate
hook.
But you'll have to take hand of another three lifecycle hooks to avoid starting an infinite loop of requests/updates. Assuming you wish to update a posts list based on props.category
changes:
state
should have two properties,category
andcurrentCategory
, set to null on the component's constructor;getDerivedStateFromProps
is needed to updatestate.category
from the newprops.category
;shouldComponentUpdate
is needed to compare bothstate.category
andstate.currentCategory
to determine if the component should be updated;getSnapshotBeforeUpdate
is needed to determine ifcomponentDidUpdate
should make an AJAX requ