Fetching Data upon Initial Rendering
You may want to display data upon initial rendering of a component; for example, when you visit YouTube, you will see some trending videos on the initial page load. Now, if we want to design a page or a component of this kind, we could use the life cycle methods that we discussed in Chapter 4, React Lifecycle Methods. We need to decide which life cycle method to use to fetch data and store it in a state.
According to the life cycle diagram (https://packt.live/2zCiT7P), the component goes through a life cycle in the following order.
The constructor()-> render()->componentDidMount. componentDidMount()
method is used for mounting steps and is the best life cycle method for fetching and manipulating data received from the server. After fetching data in the method, we update the state of the component with the data fetched within the componentDidMount()
method in order to populate the data in the content.
From the life cycle methods&apos...