Harnessing the Power of useEffect in React

A comprehensive guide to the useEffect hook in React.

Last Updated: 2022-07-01

Cover Image

Introduction to useEffect

The useEffect hook is one of the most commonly used hooks in React. It allows you to perform side effects in your function components. In this blog post, we will explore how to use useEffect and some common use cases.

Basic Usage

The useEffect hook takes two arguments: a function and a dependency array. The function is executed after the component renders, and the dependency array determines when the effect should be re-run.

import React, { useEffect, useState } from "react";
 
function ExampleComponent() {
  const [count, setCount] = useState(0);
 
  useEffect(() => {
    document.title = `You clicked ${count} times`;
  }, [count]);
 
  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>Click me</button>
    </div>
  );
}

In the above example, the document title is updated every time the count state changes.

Cleaning Up Effects

Sometimes, effects need to clean up after themselves to avoid memory leaks. You can return a cleanup function from the effect.

useEffect(() => {
  const timer = setInterval(() => {
    console.log("This will run every second");
  }, 1000);
 
  return () => clearInterval(timer);
}, []);

In this example, the interval is cleared when the component unmounts.

Conditional Effects

You can conditionally run effects by specifying dependencies in the dependency array. The effect will only re-run if one of the dependencies changes.

useEffect(() => {
  console.log("This will run only when count changes");
}, [count]);

Fetching Data

A common use case for useEffect is fetching data from an API.

useEffect(() => {
  async function fetchData() {
    const response = await fetch("https://api.example.com/data");
    const data = await response.json();
    console.log(data);
  }
 
  fetchData();
}, []);

In this example, data is fetched from an API when the component mounts.

Conclusion

The useEffect hook is a powerful tool for managing side effects in React components. By understanding how to use it effectively, you can write more predictable and maintainable code.

Happy coding!

Suggested Articles

Cover Image

Demystifying React Context

A guide to understanding and using React Context.

Cover Image

Harnessing the Power of useEffect in React

A comprehensive guide to the useEffect hook in React.

Cover Image

Mastering the useState Hook in React

The interactive guide to understanding how to manage states.

Upskill Your Frontend Development Techniques 🌟

Subscribe to stay up-to-date and receive quality frontend development tutorials straight to your inbox!

No spam, sales, or ads. Unsubscribe anytime you wish.