Concept
useEffect runs code after render to synchronise with something outside React: a subscription, a timer, the document title. Returning a function from it cleans up before the next run and on unmount.
The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.
A few lines that show useEffect and cleanup doing its job.
function useWindowWidth() {
const [width, setWidth] = useState(window.innerWidth);
useEffect(() => {
const onResize = () => setWidth(window.innerWidth);
window.addEventListener("resize", onResize);
return () => window.removeEventListener("resize", onResize);
}, []);
return width;
}Leaving a value out of the dependency array to stop the effect re-running. The effect then uses a stale value from an old render. Fix the cause: move the value inside, or make the effect not need it.
Your roadmap puts these before useEffect and cleanup, unless you already know them.
Once a session shows you understand useEffect and cleanup, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.
Topics that build on useEffect and cleanup.