Forcing myself to confront useEffect properly
Forcing myself to confront useEffect properly
Because it won't click until I force myself to write it out word for word it seems. This is why I can't stop blogging, right, come on.
I'm doing it for the wind farms. Let's go. I am always always doing it for the wind farms. My friends. I can see them out to sea - and they are soooo beautiful. Right. There are some clues in the lesson.
The Effect Hook "runs some JavaScript code after each render." Hmm.
The moments when components re-render are a good time to execute effects - effects that we will want to happen when the component re-renders, and only in certain circumstances. There are three moments when a useEffect can be executed. Here they are
- When a component is first added to the DOM - this is known as mounting
- When the state or the props of a component change - this changing causes a component to re-render
- When a component is removed from the DOM - this is known as unmounting
The Effect Hook
The Effect Hook "tells our component to do something every time it's rendered." "We can use the effect hook to create interesting dynamic changes in our web pages." So every time something appears, disappears or changes (and we can specify what and when), we can run an effect - or cause something to happen.
Clean Up Effects
These were mentioned in my homework and I had no idea what they were so I am very grateful that they are here - thanks.
"Some effects require CLEANUP. We might want to add event listeners. But then we need to remove those event listeners." Um okay why.
We would not want our program to have a new event listener be added to the DOM and the object in question every time the component re-renders. This could cause bugs, diminish performance and crash the program.
Effects run after every re-render. React calls the cleanup function before each re-render and before unmounting. Okay, I don't get this. Omg. Great thanks. Too much learning. Too much sitting still. Not enough moving. I need to dance. Cleanup functions are optional so we need to return a cleanup function from our effect. This avoids memory leaks. This makes some sense.
I am building out something to practise cleanup functions. It doesn't make any sense so I might resume tomorrow. Plus my study hour is over. It's time for my meetings. Anyway thanks! Love you and have a nice day bye
.png)
Comments
Post a Comment