0

i am a begginer to react i am learning the useEffect hook currently i am a little bit confused over the sequence at which the data in console.log is printed , please explain it in steps , thanks

initailly i see b and c printed but then i see a ,b ,c after each second why is that ? code

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


 const tick = () => {

   setCount(count + 1)
 }

 useEffect(() => {
   console.log("b")

   const intervel = setInterval(tick, 1000)

   console.log("c")
   return () => {
     console.log("a")
     clearInterval(intervel)

   }
 }, [count]);

3 Answers 3

1

The function inside which you print a is called a cleanup function. React calls that function before applying the next effects. That is why you see afterwards a printed each time count is changed. It is a cleanup being called from the previous render before applying effect for this render. From the docs:

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 render1 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.


1 That part of docs didn't talk about dependencies yet, hence it mentions effects running on each render.

Sign up to request clarification or add additional context in comments.

Comments

0

The function you are returning at the end of useEffect()

return () => {
     console.log("a")
     clearInterval(intervel)
   }

is not run the first time the component renders. That is called a cleanup function. You are returning that anonymous function to the top of the execution queue on the next render.

Knowing that, we can see that your render cycle will look something like this

Render 1:

console.log("b")

wait 1 second

console.log("c")

return console.log("a") to next render

Render 2:

console.log("a")

console.log("b")

wait 1 second

console.log("c")

return console.log("a") to next render

1 Comment

thanks a lot maan , also i dont't think there will be one second wait for b or c but there will be wait one second wait and then the useEffect runs again because its setInterval(tick,1000) it will call tick function after 1 second which increase the count by 1 , i am not sure but i think its like that , share anything if you knoq , again thanks.
0

you set that useEffect depend on [count]

it means that you ask useEffect to recall everytime count change and call returned function (

return ()=>{
console.log("a")
clearInterval(intervel)

}

) after count change so

first component render after render useEffect call this function

   console.log("b")

   const intervel = setInterval(tick, 1000)

   console.log("c")

but tick change count every 1s and useEffect depend on it so after 1s useEffect will clear because count changed so interval will stop

and will call again so it will create a new interval and start again again again

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.