1. Why is the return a function? return () => { ignore = true };

From the docs:

Why did we return a function from our effect? This is the optional cleanup mechanism for effects. Every effect may return a function that cleans up after it. This lets us keep the logic for adding and removing subscriptions close to each other. They’re part of the same effect!

and

When exactly does React clean up an effect? React performs the cleanup when the component unmounts. However, as we learned earlier, effects run for every render and not just once. This is why React also cleans up effects from the previous render before running the effects next time. We’ll discuss why this helps avoid bugs and how to opt out of this behaviour in case it creates performance issues later below.

2. What is ignored used for in this example?

Initially in useEffect Hook ignore is set like, let ignore = false;.

When fetchProduct function executes it checks for ignore is true and accordingly sets setProduct(json).

This means we have state called product and setting the value in state using setProduct(json).

This product in state is used to render details on page.

Note: As [productId] is passed as second argument to useEffect, fetchProduct function will only get executes when productId changes.

See optimizing performance by skipping effects.

Answer from ravibagul91 on Stack Overflow
🌐
Scaler
scaler.com › home › topics › react › useeffect hook
useEffect Hook in React
May 4, 2023 - This hook is called "useEffect" as it is used to perform side effects in a functional component. The term "effect" refers to any changes to the applicable state or behavior that happen as a result of the code in the hook.
🌐
Horilla
horilla.com › blogs › what-is-useeffect-hook-and-how-to-manage-side-effects-in-react
What is UseEffect Hook & How to Manage Side Effects in React? | Blogs | Free HRMS | Horilla
October 25, 2023 - The UseEffect hook is a powerful tool for managing side effects and handling component lifecycle events in React functional components. Whether you’re fetching data, setting up timers, or performing cleanup, useEffect provides a clean and ...
🌐
Kinsta®
kinsta.com › home › resource center › blog › react › demystifying react’s useeffect hook
Demystifying React's useEffect Hook - Kinsta®
October 1, 2025 - The powerful React useEffect Hook allows you to seamlessly handle side effects, such as fetching data from an API or manipulating the DOM.
🌐
Dave Ceddia
daveceddia.com › useeffect-hook-examples
How the useEffect Hook Works (with Examples)
October 22, 2020 - Let’s look at another common use case: fetching data and displaying it. In a class component, you’d put this code in the componentDidMount method. To do it with hooks, we’ll pull in useEffect.
Top answer
1 of 8
88

1. Why is the return a function? return () => { ignore = true };

From the docs:

Why did we return a function from our effect? This is the optional cleanup mechanism for effects. Every effect may return a function that cleans up after it. This lets us keep the logic for adding and removing subscriptions close to each other. They’re part of the same effect!

and

When exactly does React clean up an effect? React performs the cleanup when the component unmounts. However, as we learned earlier, effects run for every render and not just once. This is why React also cleans up effects from the previous render before running the effects next time. We’ll discuss why this helps avoid bugs and how to opt out of this behaviour in case it creates performance issues later below.

2. What is ignored used for in this example?

Initially in useEffect Hook ignore is set like, let ignore = false;.

When fetchProduct function executes it checks for ignore is true and accordingly sets setProduct(json).

This means we have state called product and setting the value in state using setProduct(json).

This product in state is used to render details on page.

Note: As [productId] is passed as second argument to useEffect, fetchProduct function will only get executes when productId changes.

See optimizing performance by skipping effects.

2 of 8
83

I will explain it here, as it took me a while to understand the explanations above, so I will try to make it simpler for others. Answers to your questions:

Why is return a function?

return () => { ignore = true };

useEffect offers the return function which is used for cleanup purposes. OK!, When do you need a cleanup? If You subscribe to something (like an event listener) and want to unsubscribe from it, this is where You should add the unsubscription logic to prevent a race condition or other issues!

What is ignore used for in this example?

ignore is used as a flag that tells your effect to ignore, or not, the API call. When is that used? When a race condition might occur.

Example of a race condition:

Imagine You have a list of products, and You fetch product details when a product is clicked. If You click on products quickly, multiple API calls might be in progress simultaneously, resulting in changes to product_id that would trigger useEffect to run multiple times. Here's what could happen:

  1. Click on product1: an API call for product1 will trigger.
  2. Quickly click on product2: triggers API call for product2.
  3. API call for product2 finishes first and the page updates with product2 data
  4. API call for product1 finishes later, and the page updates with product1 data.

Now data for product1 is displayed on the page, even though product2 was clicked last. This is a race condition.

BUT WHAT DOES THAT HAVE TO DO WITH ignore??

Logic explanation

ignore, my dear, tells setProduct to ignore the data from product1, and to not update the state with It, or in other words, It is used to ensure outdated API calls don't update the state when a race condition occurs.

Code explanation

First scenario : No race condition

  1. Click on product1:
    • ignore = false
    • call API for product1
    • Data arrives
    • If (!ignore) = true -> update state with product1 data
    • Set ignore = true inside cleanup function.
    • Congrats! product1 data is displayed correctly.

Second scenario: Race condition

  1. product1 is clicked
    • ignore = false
    • call API for product1
    • data still hasn't arrived
  2. Quickly click on product2
    • ignore = false
    • call API for product2
  3. Data for product2 arrives first
    • !ignore is true -> update state with product2 data
    • Cleanup function sets ignore = true
  4. Data for product1 arrives later
    • if (!ignore) = false (because ignore = true)
    • Oops, old data is ignored, so product1 data does not update the state.

And this is how we will always have up-to-date data.

🌐
DEV Community
dev.to › nibble › what-is-useeffect-hook-and-how-do-you-use-it-1p9c
What is useEffect hook and how do you use it? - DEV Community
June 30, 2020 - A hook is a function which enables you use state and other react features without writing ES6 classes. useEffect hook is part of the react hooks API. If you are familiar with react life cycles, useEffect hook is equivalent to life cycle methods ...
🌐
NamasteDev
namastedev.com › home › react › react useeffect hook deep dive
React useEffect Hook Deep Dive - NamasteDev Blogs
July 3, 2025 - Before React introduced hooks, side effects were primarily handled using lifecycle methods in class components (like componentDidMount, componentDidUpdate, and componentWillUnmount). The useEffect hook consolidates these methods into a single API.
Find elsewhere
🌐
Medium
caelinsutch.medium.com › react-useeffect-hook-in-depth-dc6b7c6132e5
React useEffect Hook in Depth. A thorough analysis of the useEffect… | by Caelin Sutch | Medium
December 21, 2020 - If you just want the hook to run on original mount and unmount, just use an empty dependency array. That was a lot of information, so lets recap what we learned. useEffect is a React hook to make side effects in functional components.
🌐
Medium
medium.com › @urza.p › react-useeffect-hook-what-why-and-how-to-use-it-9730c28c93bd
React useEffect Hook: What, Why, and How to Use It | by Urja | Medium
June 4, 2025 - In simple terms, useEffect is a React hook that lets you handle side effects — things that happen outside the normal rendering process.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-use-the-usestate-and-useeffect-hooks-in-your-project
React Hooks – How to Use the useState & useEffect Hooks in Your Project
October 8, 2024 - In the code example above, MyComponent is a functional component which utilizes React hooks, specifically useState and useEffect, to handle state management and execute side effects. The useState hook is used to initialize a state variable called data.
🌐
Educative
educative.io › blog › react-useeffect-hook
Advanced React Hooks: Deep dive into the useEffect Hook
March 5, 2025 - useEffect is one of the most popular Hooks that allows you to create conditional changes that occur even after your app is rendered. Today, we take a deeper look at when and how to use this Hook in your own apps.
🌐
Mimo
mimo.org › glossary › react › useeffect-hook
React useEffect Hook: Syntax, Usage, and Examples
The React useEffect hook lets you synchronize a component with external systems. It replaces lifecycle methods like componentDidMount, componentDidUpdate, and componentWillUnmount from class-based components.
🌐
freeCodeCamp
freecodecamp.org › news › react-hooks-useeffect-usestate-and-usecontext
How to Use React Hooks – useEffect, useState, and useContext Code Examples
December 4, 2023 - The cleanup function returned by useEffect clears the interval when the component is unmounted. The useContext hook is used to consume values from a React context. Context provides a way to pass data through the component tree without having to pass props manually at every level.
🌐
Medium
medium.com › @ralph1786 › how-to-use-the-useeffect-hook-ec2e77d0240e
How To Use The useEffect Hook. Welcome back reader to another blog… | by Rafael Cruz | Medium
September 20, 2019 - As I mentioned before the useEffect hook is your equivalent to the ComponentWillUnmount lifecycle method where you will do some cleanup work like removing event listeners. To do some cleanup with the useEffect hook you must return a cleanup function within the callback function you pass as the first argument to the useEffect hook.
🌐
Stackademic
blog.stackademic.com › mastering-reacts-useeffect-hook-a-deep-dive-c8da06c46771
Mastering React’s useEffect Hook: A Deep Dive | by Aditya Armal | Stackademic
December 8, 2023 - To wrap up, the useEffect hook is a game-changer in simplifying side effects in React components. It streamlines the process of running side effects when props or state change and offers a cleaner organization of side effects, assigning each ...
🌐
Dmitri Pavlutin
dmitripavlutin.com › react-useeffect-explanation
A Simple Explanation of React.useEffect()
January 28, 2023 - useEffect() hook executes side-effects in React components.
🌐
DEV Community
dev.to › cassidoo › when-useeffect-runs-3pf3
When useEffect runs - DEV Community
June 4, 2024 - ===> This hook runs synchronously after the browser render <=== Don't use useLayoutEffect unless you absolutely need it as it will block the JS main thread, which would degrade your app performance. The browser will repaint the screen and the user will see the changes. Once the main thread is "idling", the callbacks will be run in the order they were registered. Again, this is NOT when useEffect is run, it ran much earlier, it's just the callback that was scheduled that runs at this time.
🌐
React
react.dev › reference › react › useLayoutEffect
useLayoutEffect – React
HooksCopy pageCopy · useLayoutEffect can hurt performance. Prefer useEffect when possible. useLayoutEffect is a version of useEffect that fires before the browser repaints the screen. useLayoutEffect(setup, dependencies?) Reference · useLayoutEffect(setup, dependencies?) Usage ·
🌐
LogRocket
blog.logrocket.com › home › how to use the useeffect hook in react: a complete guide
How to use the useEffect hook in React: A complete guide - LogRocket Blog
June 3, 2025 - Learn how to use the useEffect Hook in React to manage side effects, handle async tasks, and avoid common mistakes with real-world examples.