Fetching Data on Update
We now have a good idea of which life cycle method we need to use while fetching data upon initial rendering. In this section, we are going to learn which life cycle method we need to use while fetching data while a component gets updated.
Suppose a child component is fetching data from the server with the values sent from the parent component. Now, if we update the state of the parent component and use the componentDidMount()
method in the child component to fetch data from the server simultaneously, we won't be able to do so even if the child components are re-rendered. This is because the componentDidMount()
method is only called once in the life cycle of any component (discussed in Chapter 4, React Lifecycle Methods in React).
The following diagram shows an example of a parent and child component:
The parent component has a state value of 134
and we send this value...