Ways to mimic lifecycle methods using useEffect hook

We know that the useEffect() is used for causing side effects in functional components and it is also capable of handling componentDidMount(), componentDidUpdate(), and componentWillUnmount() life-cycle methods of class-based components into the functional components.

useEffect(()=>{
//You can add your code here for mounting phase of component
console.log("Mounting in Functional Component")
},[])
// adding an empty array ensures that the useEffect is only triggered once
// (when the component mounts)

useEffect(()=>{
//You can add your code for updating phase of component
console.log("Updating in Functional Component")
},[values])
//values triggers re render whenever they are updated in your program,
//you can add multiple values by separating them by commas

useEffect(()=>{
return()=>{
//You can add your code for unmounting phase of component
console.log("Functional Component Removed ")
}
},[])
//Write all the code of unmounting phase only inside the callback function



ReactJS useEffect Hook

React useEffect hook handles the effects of the dependency array. The useEffect Hook allows us to perform side effects on the components. fetching data, directly updating the DOM and timers are some side effects. It is called every time any state if the dependency array is modified or updated.

Table of Content

  • What is useEffect hook in React?
  • Why choose useEffect hook?
  • How does it work?
  • Importing useEffect hook
  • Structure of useEffect hook
  • Controlling side effects in useEffect
  • Ways to mimic lifecycle methods using useEffect hook

Similar Reads

What is useEffect hook in React?

The useEffect in ReactJS is used to handle the side effects such as fetching data and updating DOM. This hook runs on every render but there is also a way of using a dependency array using which we can control the effect of rendering...

Why choose useEffect hook?

useEffect hook is used to handle side effects in functional components, such as fetching data, updating the DOM, and setting up subscriptions or timers. It is used to mimic the lifecycle methods of class-based components. The motivation behind the introduction of useEffect Hook is to eliminate the side effects of using class-based components. For example, tasks like updating the DOM, fetching data from API end-points, setting up subscriptions or timers, etc can lead to unwarranted side effects. Since the render method is too quick to produce a side-effect, one needs to use life cycle methods to observe the side effects....

How does it work?

You call useEffect with a callback function that contains the side effect logic. By default, this function runs after every render of the component. You can optionally provide a dependency array as the second argument. The effect will only run again if any of the values in the dependency array change....

Importing useEffect hook

To import the useEffect hook, write the following code at the top level of your component...

Structure of useEffect hook

The useEffect hook syntax accepts two arguments where the second argument is optional...

Controlling side effects in useEffect :

1. To run useEffect on every render do not pass any dependency...

Ways to mimic lifecycle methods using useEffect hook

...