Welcome! Today, we'll traverse React's cosmos, exploring the useEffect
Hook. We'll delve into the basics of useEffect
, unravel the concept of side effects, and decipher the dependency array. Additionally, we'll understand how useEffect
behaves in three scenarios: when it has no dependency array, an empty array, and a non-empty array. Are you ready? Let's begin!
First, let's demystify useEffect
. It's a React Hook that introduces effects to functional components. "Effects" encapsulate the actions or tasks that occur as a component renders, updates, or unmounts. But before we proceed, we should briefly revisit the useState
Hook; useState
provides a "state"—think of it as short-term memory—for functional components.
Consider a simple web application that displays a counter, incrementing each time the user clicks a button. In addition to counting clicks, we want to log a console message each time the counter updates. To accomplish this, we utilize useEffect
:
JavaScript1import React, { useState, useEffect } from 'react'; 2 3function CounterApp() { 4 // useState is used to add a counter state to our component 5 // setCounter is a function to update the current counter 6 const [counter, setCounter] = useState(0); 7 8 useEffect(() => { 9 // useEffect logs the current counter value each time it changes 10 console.log(`Counter value: ${counter}`); 11 }, [counter]); // observe changes in the counter 12 13 return ( 14 <div> 15 <p>Counter: {counter}</p> 16 <button onClick={() => setCounter(counter + 1)}> 17 Increase Count 18 </button> 19 </div> 20 ); 21} 22export default CounterApp;
The dependency array, which is the second argument of useEffect
, informs useEffect
which states or props should be watched. useEffect
re-runs whenever these watched entities change.
What happens when useEffect
lacks a dependency array? It runs after every render, regardless of which changes triggered the render.
JavaScript1useEffect(() => { 2 console.log(`Component rendered.`); 3});
Here, "Component rendered" is logged on every render because useEffect
doesn't watch any specific state or props for changes.
The empty dependency array for a useEffect
Hook has a special meaning. When useEffect
is used with an empty array, it runs the effect only once when the component "mounts" (i.e., appears on the screen for the first time).
This behavior can be compared to a spaceship's liftoff sequence. Just as a spaceship needs an initial push or thrust to take off from the launchpad, a useEffect
Hook with an empty dependency array hopes to perform an initial action just once when the component first renders.
Here is an example:
JavaScript1useEffect(() => { 2 console.log("Component did mount"); 3}, []);
In the code above, the message "Component did mount" will be logged only once, right after the component is rendered for the first time.
If the dependency array includes states or props, useEffect
observes these entities and re-runs whenever they change:
JavaScript1useEffect(() => { 2 console.log(`Counter value: ${counter}`); 3}, [counter]);
In this context, useEffect
logs every change in the counter
value to the console.
You may have heard of the term "clean up" in programming. Imagine a mission in space, when it's time to return to Earth, the spaceship must leave the orbit and descend. Similarly, in programming, a function often needs to clean up or tidy up after itself when it's finished.
In useEffect
, the cleanup function is what allows your component to undo whatever changes it made to the environment before it disappears from the screen, or "unmounts". Providing a cleanup function helps to prevent memory leaks, provides smoother user experiences, and helps to keep your code organized and easy to reason about.
You can think of the cleanup function as the mission's end when the spaceship lands back on Earth and the astronauts exit the spaceship. Here's how you can provide a cleanup function in your useEffect
Hook:
JavaScript1useEffect(() => { 2 console.log("Component did mount OR update"); 3 4 // cleanup function 5 return () => { 6 console.log("Component will unmount"); 7 } 8}, []);
In this example, the message "Component will unmount" is logged when the component instances are about to disappear from the screen, or in our analogy, when the spaceship is about to land.
Excellent work! You've conducted a thorough exploration of useEffect
in React. Next, prepare for hands-on exercises to solidify your newly acquired skills. Remember, practice transitions knowledge into skill. Let's continue learning together as we traverse the vast cosmos of coding. Happy coding!