Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the expected return of `useEffect` used for?

In the React documentation for hooks they say:

"This also allows you to handle out-of-order responses with a local variable inside the effect"

useEffect(() => {     let ignore = false;     async function fetchProduct() {       const response = await fetch('http://myapi/product/' + productId);       const json = await response.json();       if (!ignore) setProduct(json);     }      fetchProduct();     return () => { ignore = true };   }, [productId]); 

Demo app

Please help me understand this better by explaining:

  1. Why is the return a function? return () => { ignore = true };
  2. What is ignored used for in this example?

Thanks!

like image 394
PrimeTimeTran Avatar asked Jun 28 '19 03:06

PrimeTimeTran


People also ask

What is the purpose of return in useEffect?

Anatomy of the useEffect hook The return function is the cleanup function, or when the user leaves the page and the component will unmount. The array is the last part, and it is where you put the states that will update throughout the component's lifecycle.

What is return type of useEffect?

Whenever the input changes, state is set and hence useEffect runs again, which invokes a new API call. To ignore the old API call result, the returned function of useEffect sets the previous useEffect localvariable to false.

What is useEffect used for?

What does useEffect do? By using this Hook, you tell React that your component needs to do something after render. React will remember the function you passed (we'll refer to it as our “effect”), and call it later after performing the DOM updates.

Can useEffect return a value?

you can return your value and put in state and use it outside useEffect. if your value need preState, remember to pass an arrow function in setState and pass preState as a parameter.


2 Answers

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 behavior in case it creates performance issues later below.

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.

like image 77
ravibagul91 Avatar answered Sep 24 '22 16:09

ravibagul91


I will explain it here, as it took me a while to understand the explanations above, so I will try to make it simpler on others.

Answers for your questions:
1- Why is the return a function? return () => { ignore = true };
useEffect offers the use of return function, which is used for cleanup function purposes, OK!, When do you need a clean up? If u made a subscription for something, and u want to unsubscribe from it for example, you should add unsubscription logic in "return function" inside useEffect , instead of putting the logic in other places, that may cause a race condition!

2- What is ignored used for in this example?
The ignore is used as a flag that tells the function to ignore the api call or not. when is that used? when you have a race condition.

[example for a race condition - you can ignore this part of u are already familiar with race conditions] For example u click in list of products, and when each product is clicked you will update the page with this product info, if u click quickly in the products for many times , the useEffect will be updated whenever product_id is changed, and at some point clicking on product1 [will call api for product1 ]then quickly product2 [will call api for product2 and retrieve data causing -> setProduct(product2data) ] while still data for product1 has just arrived causing -> setProduct(product1data) . This way we have product2 clicked ! but product1 data appears at page!!.

BUTTTT WHAT THAT HAVE TO DO WITH ignore ??
[logic explanation]
ignore , my dear , tells the setProduct to ignore the old data, and not set it, this way we only will set the last clicked product, even if old data arrived later.
[code explanation]
1- first scenario : no races , product1 is clicked -> ignore is false , call api , retrieve data , if(!ignore ) -> will set product1 data , now clean up function will set ignore to true , congrats !.

2- second scenario -> race condition , product1 is clicked -> ignore is false, call api , [data still hasn't arrived] ,now product2 is clicked ignore is false , call api , data arrived , if(!ignore ) -> will set product2 data , clean up function will set ignore to true , product1 function data has arrived -> if(!ignore ) -> is false OOOPS, will not set product1 data "old data".

AND THIS WAY WE WILL ALWAYS HAVE NEW DATA . CHEERRRRRRRRS :D

like image 37
rawand deheliah Avatar answered Sep 22 '22 16:09

rawand deheliah