A Comprehensive Guide to Using useEffect() Hook in React

A Comprehensive Guide to Using useEffect() Hook in React

Welcome back to my React Hooks series! In this article, we'll be diving into the useEffect hook, which is an essential tool for managing side effects in functional components. If you missed my previous article on the useState hook, be sure to check it out here. You can also find all of the articles in this series on my Hashnode publication, Dear Dev Diary.

In this article, we'll be covering the syntax and parameters of useEffect, its common use cases, tips and best practices for using it effectively, and examples of how to implement it in different scenarios. By the end of this article, you'll have a solid understanding of how useEffect works and how you can use it to make your React applications more powerful and efficient.

Make sure to follow me on Hashnode to stay up-to-date with the latest articles in this series and more React-related content. Let's get started!

useEffect() Hook

In React, side effects like fetching data, manipulating the DOM, or setting up subscriptions are typically implemented in class components using lifecycle methods like componentDidMount, componentDidUpdate, and componentWillUnmount. However, with the introduction of hooks in React 16.8, we can now manage state and side effects in functional components as well.

The useEffect hook is one of the most powerful and versatile hooks in React, allowing us to manage side effects in our functional components. In this article, we'll explore the ins and outs of the useEffect hook, including how it works, how to use it, and some common use cases.

Syntax and Parameters

The useEffect hook takes two parameters: a function and an optional array of dependencies. The function represents the side effect we want to execute, and the dependencies control when the effect should be executed.

import { useEffect } from 'react';

useEffect(() => {
  // side effect code
}, [dependencies]);

The first parameter is a function that will be executed after every render cycle. It can contain any logic or code that needs to be executed after the component is rendered. The second parameter is an array of dependencies, which controls when the effect should be executed. The effect will be executed when any of the dependencies change, or when the component mounts or unmounts. If the array is empty, the effect will only be executed once, after the initial render.

Here's an example:

import { useState, useEffect } from 'react';

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

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

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

In this example, we're using useEffect to update the document title with the current count every time it changes. The count variable is included in the dependencies array, so the effect will only be executed when the count variable changes.

That's a basic overview of the syntax and parameters of the useEffect hook. In the next section, we'll explore some common use cases for useEffect.

Real world example of useEffect()

Let's say you are building a chat application with React. In this chat app, you want to show the number of unread messages in the document title. You can use useEffect() to achieve this.

First, you need to define a state to store the number of unread messages. You can define it like this:

const [unreadCount, setUnreadCount] = useState(0);

Then, you can use useEffect() to update the document title whenever the unreadCount changes:

useEffect(() => {
  document.title = `You have ${unreadCount} unread messages`;
}, [unreadCount]);

In this example, the useEffect() hook is called with two arguments. The first argument is a function that updates the document title, and the second argument is an array that contains the unreadCount state variable. This means that the effect will only run when the unreadCount variable changes.

So, whenever the unreadCount state changes, the effect will be triggered and the document title will be updated with the new count of unread messages.

This is just one example of how you can use useEffect() in a real-world application. There are many other use cases, such as fetching data from an API, subscribing to a WebSocket, or cleaning up after a component unmounts.

Common Use Cases

There are several common use cases for useEffect() in a React application. Here are a few:

  1. Fetching Data: One of the most common use cases for useEffect() is fetching data from an API. You can use useEffect() to make an API call when the component mounts and then store the result in a state variable.

     import React, { useState, useEffect } from 'react';
    
     function MyComponent() {
       const [data, setData] = useState([]);
    
       useEffect(() => {
         fetch('<https://api.example.com/data>')
           .then(response => response.json())
           .then(data => setData(data));
       }, []);
    
       return (
         <ul>
           {data.map(item => <li key={item.id}>{item.name}</li>)}
         </ul>
       );
     }
    
  2. Subscribing to a WebSocket: You can also use useEffect() to subscribe to a WebSocket and receive real-time data. When the component mounts, you can create a WebSocket connection and then update the state variable whenever new data is received.

     import React, { useState, useEffect } from 'react';
    
     function MyComponent() {
       const [data, setData] = useState([]);
    
       useEffect(() => {
         const ws = new WebSocket('wss://api.example.com');
    
         ws.addEventListener('message', event => {
           const newData = JSON.parse(event.data);
           setData([...data, newData]);
         });
    
         return () => {
           ws.close();
         };
       }, []);
    
       return (
         <ul>
           {data.map(item => <li key={item.id}>{item.name}</li>)}
         </ul>
       );
     }
    
  3. Setting Up Event Listeners:

    You can use useEffect() to set up event listeners when a component mounts. For example, you can use useEffect() to add a scroll event listener to the window object.

     import React, { useEffect } from 'react';
    
     function MyComponent() {
       useEffect(() => {
         function handleScroll() {
           console.log('User scrolled!');
         }
    
         window.addEventListener('scroll', handleScroll);
    
         return () => {
           window.removeEventListener('scroll', handleScroll);
         };
       }, []);
    
       return (
         <div>
           <h1>Welcome to My Page</h1>
         </div>
       );
     }
    
  4. Cleaning Up After a Component Unmounts: You can also use useEffect() to clean up after a component unmounts. This is particularly useful for removing event listeners, cancelling API requests, or closing WebSocket connections.

     import React, { useState, useEffect } from 'react';
     import axios from 'axios';
    
     function Example() {
       const [data, setData] = useState(null);
    
       useEffect(() => {
         const intervalId = setInterval(() => {
           axios.get('<https://jsonplaceholder.typicode.com/todos/1>').then(response => {
             setData(response.data);
           });
         }, 1000);
    
         return () => {
           clearInterval(intervalId);
         };
       }, []);
    
       return (
         <div>
           <h1>Todo:</h1>
           <p>{data ? data.title : 'Loading...'}</p>
         </div>
       );
     }
    

    In this example, useEffect sets up an interval to fetch data every second using setInterval and axios. It updates the data state with the response data, which is then displayed in the component. It also returns a cleanup function that clears the interval using clearInterval when the component unmounts, ensuring that the interval doesn't continue to run unnecessarily.

    Understanding Side Effect

    In React, a side effect is any operation that affects something outside of the component's render cycle, such as updating the DOM, making an API call, or setting a timer.

    When building complex applications, it's common to need to perform side effects in response to changes in state or props. This is where the useEffect hook comes in handy.

    The useEffect hook allows you to perform side effects in function components. It takes two arguments: a callback function to perform the side effect, and a dependency array to specify the dependencies of the effect. The effect runs after the component has rendered and after every re-render if the dependencies have changed.

    By using useEffect, you can keep your side effects in one place and avoid cluttering your component with side-effect related code. This can make your code easier to read and maintain.

    Here's an example:

    Suppose you have a component that displays a list of items fetched from an API. You want to update the list every time a certain prop changes, but you also want to make sure that you clean up any resources (like an interval) when the component unmounts. Here's how you could use useEffect to achieve this:

     import React, { useState, useEffect } from 'react';
     import fetchData from './api/fetchData';
    
     function List(props) {
       const [items, setItems] = useState([]);
    
       useEffect(() => {
         const interval = setInterval(() => {
           fetchData(props.filter).then((data) => setItems(data));
         }, 5000);
    
         return () => clearInterval(interval);
       }, [props.filter]);
    
       return (
         <ul>
           {items.map((item) => (
             <li key={item.id}>{item.name}</li>
           ))}
         </ul>
       );
     }
    

    In this example, we're using useEffect to set up an interval that fetches the data from the API every 5 seconds. We're also using the props.filter value as a dependency, so the effect will re-run every time the filter changes. When the component unmounts, the cleanup function returned by useEffect is called, which clears the interval. This ensures that we're not leaving any resources running after the component has been removed from the DOM.

    Note that we're also updating the state with the fetched data in the then block of the fetchData promise. This is necessary to trigger a re-render of the component and update the list with the new data.

    Tips and Best Practices

    Some tips and best practices to keep in mind while using the useEffect hook in React:

    1. Always define dependencies: The second parameter of the useEffect hook is the array of dependencies. It is important to define these dependencies so that your effect only runs when any of these dependencies change. If you don't define the dependencies, your effect will run on every render, which can cause performance issues.

    2. Avoid infinite loops: If you modify the state inside an effect, it will trigger a re-render, which will run the effect again. If you don't define your dependencies properly, this can lead to an infinite loop. To avoid this, you can use useEffect inside a useMemo or useCallback hook, or use the useLayoutEffect hook instead.

    3. Cleanup your effects: If your effect performs any asynchronous operation, like setting a timer or fetching data, it is important to clean up after it. You can use the return statement inside the effect to define a cleanup function, which will run when the component unmounts or when the dependencies change.

    4. Use separate effects for separate concerns: If you have multiple side effects in a component, it's best to separate them into different useEffect hooks. This makes your code more modular and easier to understand.

    5. Avoid side effects in the render method: Side effects should not be performed inside the render method, as it can cause issues with rendering and can lead to infinite loops.

    6. Keep the effect simple: It's a good practice to keep your effects as simple as possible. If your effect is too complex, it can make your code harder to understand and maintain.

    7. Use useEffect only when necessary: If you're just updating the state based on a prop change, it's better to use a useState hook instead of useEffect.

Remember, the goal of useEffect is to manage side effects in your React components. By following these tips and best practices, you can write clean and efficient code that works seamlessly with React's rendering system.

Gotchas

Some potential "gotchas" to be aware of when using useEffect:

  1. Dependency arrays: The second argument of useEffect is a dependency array, which specifies what state or props the effect depends on. It's important to ensure that you include all relevant dependencies in this array, as omitting dependencies can lead to bugs and unexpected behavior.

  2. Multiple effects: You can have multiple useEffect calls in a single component, but be careful to ensure that they don't conflict with each other or have overlapping dependencies.

  3. Asynchronous code: If you're using useEffect to manage asynchronous code, be sure to handle any errors and make sure that the effect cleans up after itself.

  4. Performance: Keep in mind that useEffect can be a performance-intensive operation, especially if you're using it to update state frequently. If you're experiencing performance issues, consider optimizing your code or using other hooks like useCallback and useMemo to improve performance.

  5. Avoiding infinite loops: If your useEffect updates state in a way that triggers the effect again, you can end up with an infinite loop. To avoid this, be careful to specify the right dependencies in the dependency array and use conditional logic where necessary.

By being aware of these potential pitfalls, you can use useEffect effectively and avoid common issues that can arise when working with side effects in React.

Conclusion

In conclusion, the useEffect hook is a powerful tool that can help manage side effects in React. By allowing you to handle component lifecycle events and perform tasks after rendering, it can help keep your code organized and efficient. However, it's important to keep in mind best practices and potential gotchas to avoid common mistakes and bugs. By using useEffect effectively, you can create more robust and reliable React applications.

Did you find this article valuable?

Support Animesh Sharma by becoming a sponsor. Any amount is appreciated!