Why do we need useEffect hook?

useEffect hook is used to run side-effects in the React component. What is considered as side-effects in a UI?

Anything other than DOM rendering is side-effects. Some of the side effects are,

  • Fetching data through Ajax calls
  • Registering an event listener
  • Doing DOM manipulations
  • Sending analytics data to server based on state changes
  • Timers in the code (i.e., setInterval, setTimeout)

So in short, if the react component needs to do anything after rendering the component, those will be side-effects. useEffect hook will help to run such side-effects in React and handle memory management effectively.

Add your comments about how you have used useEffect in your code šŸ˜Ž