React Course: Everything About useEffect

React is one of the most popular JavaScript libraries for building modern web applications. Among them, the useEffect hook plays a very important role in controlling the component’s lifecycle and managing side effects. In this article, we will cover the basic concepts, usage, and various examples of useEffect in detail.

1. What is useEffect?

useEffect is one of the hooks provided by the React library, used to create side effects in functional components. Side effects include data fetching, manual DOM manipulation, setting timers, or interacting with other external systems.

React components are designed to only define state and props, but sometimes there is a need to perform other actions when the component is rendered. To perform these actions, we use the useEffect hook.

2. Basic usage of useEffect

useEffect can be used with the following syntax:

useEffect(() => {
  // Side effect code
}, [dependency array]);

In the above syntax, the first parameter is the function that performs the side effects, and the second parameter is the dependency array. The dependency array includes the states or props on which this hook depends.

2.1. Simple example

import React, { useEffect, useState } from 'react';

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

  useEffect(() => {
    document.title = `Count: ${count}`;
  }, [count]);

  return (
    

Current count: {count}

); }

In the above example, the document title is updated whenever count changes. Since count is included in the dependency array, the side effect is executed only when the setCount function is called and count is updated.

3. Timing of useEffect execution

useEffect executes after the component has rendered. In summary:

  • Executed after validation when the component first renders
  • Re-executed when a value specified in the dependency array changes

For example, if the dependency array is empty:

useEffect(() => {
  // Executes only after the first render
}, []);

3.1. Cleanup function

useEffect can return a cleanup function. This cleanup function is called when the component is unmounted or before the next effect runs. This is very useful for preventing memory leaks.

useEffect(() => {
  const timer = setTimeout(() => {
    console.log('Timer executed!');
  }, 1000);

  return () => {
    clearTimeout(timer); // Clear timer when component unmounts
  };
}, []);

4. Advanced usage of useEffect

Having learned the basic usage, let’s move on to advanced concepts of useEffect. We will cover passing detailed parameters for object management, conditional execution, and more.

4.1. Conditional execution

Sometimes, you may want useEffect to execute only when specific conditions are met. This can be implemented using a conditional statement:

useEffect(() => {
  if (count > 0) {
    console.log('Count is positive');
  }
}, [count]);

4.2. Using multiple useEffect

You can use multiple useEffect in a single component to manage each effect independently. Each effect can have its own dependency array.

useEffect(() => {
  // Fetching data
}, [url]);

useEffect(() => {
  // Clear timer
  return () => {
    clearInterval(timerId);
  };
}, []);

5. Case studies of useEffect

Now let’s look at the usefulness of useEffect through real-world use cases.

5.1. API data fetching

Using useEffect is common when fetching API data in a React application:

import React, { useEffect, useState } from 'react';

function DataFetchingComponent() {
  const [data, setData] = useState([]);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    fetch('https://api.example.com/data')
      .then((response) => response.json())
      .then((data) => {
        setData(data);
        setLoading(false);
      });
  }, []);

  if (loading) return 

Loading...

; return (
    {data.map(item =>
  • {item.name}
  • )}
); }

5.2. Real-time data updates

When updating real-time data through WebSockets or Server-Sent Events (SSE), useEffect can be used:

import React, { useEffect, useState } from 'react';

function RealTimeComponent() {
  const [messages, setMessages] = useState([]);

  useEffect(() => {
    const socket = new WebSocket('ws://your-websocket-url');

    socket.onmessage = (event) => {
      const newMessage = JSON.parse(event.data);
      setMessages(prevMessages => [...prevMessages, newMessage]);
    };

    return () => {
      socket.close();
    };
  }, []);

  return (
    
{messages.map((msg, index) => (
{msg.content}
))}
); }

6. useEffect vs Lifecycle Methods in Class Components

The useEffect of functional components serves the role of lifecycle methods used in class components (componentDidMount, componentDidUpdate, componentWillUnmount).

For example, the use of useEffect that implements componentDidMount and componentWillUnmount looks like this:

useEffect(() => {
  // Executed when the component is mounted
  fetchData();

  return () => {
    // Executed when the component is unmounted
    cleanup();
  };
}, []);

7. Optimization and performance considerations

When using useEffect, you should consider its impact on performance. Here are some tips to optimize performance:

  • Set the dependency array correctly to prevent unnecessary re-renders.
  • If an effect should only run when certain states change, make that clear.
  • Use cleanup functions appropriately to release persistent memory resources.

8. Frequently Asked Questions (FAQ)

8.1. When is useEffect called?

useEffect is called when the component is mounted or when dependencies are updated.

8.2. Can useEffect be used multiple times?

Yes, multiple useEffect can be used within a single component. Each useEffect is independent and can perform different roles.

8.3. What happens if the dependency array of useEffect is left empty?

If the dependency array is set to an empty array, it will execute only when the component is first mounted and not afterwards.

8.4. Is it okay to call setState inside useEffect?

Yes, you can call setState to update state, which will cause the component to re-render. However, manage dependencies properly to avoid infinite loops.

9. Conclusion

In this tutorial, we thoroughly explored the useEffect hook in React. useEffect is an essential tool for managing side effects and is commonly used in React applications. It may seem difficult at first, but you will naturally become familiar with it as you continue to use it.

Through this article, I hope you have gained an understanding of the powerful features and usage of useEffect and can apply it to your own projects. Practice various use cases like the examples mentioned above to gain additional experience!