ReactJS useEffect Hook (original) (raw)

Last Updated : 12 Apr, 2025

The useEffect hook is one of the most commonly used hooks in ReactJS used to handle side effects in functional components. Before hooks, these kinds of tasks were only possible in class components through lifecycle methods like componentDidMount, componentDidUpdate, and componentWillUnmount.

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. It can be used to perform actions such as:

**Syntax

useEffect(() => { // Code to run on each render return () => { // Cleanup function (optional) }; }, [dependencies]);

**How does it work?

**Now let's see how to implement useEffect Hook in ReactJS

JavaScript ``

//HookCounterOne.js

// useEffect is defined here

import { useState, useEffect } from "react";

function HookCounterOne() { const [count, setCount] = useState(0);

useEffect(() => {
    document.title = `You clicked ${count} times`;
}, [count]);

return (
    <div>
        <button onClick={() => setCount((prevCount) => prevCount + 1)}>
            Click {count} times{" "}
        </button>
    </div>
);

} export default HookCounterOne;

`` JavaScript `

//App.js

// Importing and using HookCounterOne

import React from "react"; import "./App.css"; import HookCounterOne from "./components/HookCounterOne";

function App() { return (

); } export default App;

`

**Output

Animation1

**In this example

Controlling side effects in useEffect

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

useEffect(()->{
// Example Code
})

2. To run useEffect only once on the first render pass any empty array in the dependecy

useEffect(()->{
// Example Code
}, [] )

3. To run useEffect on change of a particular value. Pass the state and props in the dependency array

useEffect(()->{
// Example Code
}, [props, state] )

**Ways to mimic lifecycle methods using useEffect hook

The useEffect() hook is not only used for handling side effects, but it also allows functional components to replicate the behavior of class-based lifecycle methods like:

useEffect(() => { console.log("Component mounted (Functional)"); }, []);

useEffect(() => { console.log("Component updated (Functional)"); }, [value1, value2]);

useEffect(() => { return () => { console.log("Component unmounted (Functional)"); }; }, []);

**Cleaning Up Side Effects

useEffect allows you to clean up after your effects by returning a cleanup function. This is particularly useful for:

For example, if you're setting up a timer, you can clean it up when the component unmounts or when dependencies change.

Best Practices for useEffect

Always provide a dependency array: This helps React know when the effect should run. If you don’t specify dependencies, it will run on every render.

Conclusion

The useEffect hook is a powerful tool in React for handling side effects in function components. By using useEffect, you can easily manage tasks like data fetching, subscribing to events, and cleaning up resources when a component unmounts. Its flexibility allows you to run effects after the component renders, only when certain dependencies change, or once when the component mounts.