Learning More useEffect

Learning  More useEffect

Firstly it clicked for me while I was brushing my hair this morning. It's so simple. You specify a function to call (maybe functions? I'm not sure. Can you have more than one? I don't know yet. I need to find out), and then you specify the conditions for it to run.
The variables that change and so on.
Or nothing. Or just on mount. An empty dependency array. I don't know. I don't know. Arrrgh.

Learning More useEffect

Right, let's take a look at the lesson.

"If our effect returns a function, then the useEffect() Hook always treats that as the cleanup function."

"React will call this cleanup function before the component re-renders or unmounts."
"Since this cleanup function is optional, it is our responsibility to return a cleanup function from our effect when our effect code could create memory leaks." A mentor told me that he has never ever used or seen a cleanup function used in his whole career. Um. 

Hm.

The Dependency Array

The second argument inside of the use Effect Hook is known as the dependency array. If we only want the effect that we are calling to run after our first render, we pass in an empty array as the dependency array. "The dependency array is used to tell the useEffect() method when to call our array and when to skip it. Our effect is always called after the first render but can only be called again if something in our dependency array has changed values between renders." 

Yeah that makes sense. And then we put something into our dependency array. The variables whose values we want to cause the useEffect to run when they change." 

We'll focus on:

  • Using an empty dependency array to call an effect when a component first mounts
  • And - if a cleanup function is returned by our effect - calling that when the component unmounts
But nobody uses these functions anyway. Hmm. 
A futuristic image

I asked my colleagues about this.
Also after half an hour of react I just feel like doing some GraphQL so let me see anyway
Anyway thanks
Thanks

Comments

Popular posts from this blog

Hello World

“But yesterday, I heard God say, you were born to be the one…”

In the Water, I find Fire