ReactQuestion 17 of 28
useEffect
Answer out loud or in your head first, then compare.
Answer
It's for synchronising a component with an external system.
E.g.:
- an event listener,
- a WebSocket,
- a timer,
- integrating a third-party library.
JavaScript
useEffect(() => {
const handler = () => {};
window.addEventListener("resize", handler);
return () => {
window.removeEventListener("resize", handler);
};
}, []);Cleanup runs before the effect runs again and on unmount.
Good to know: not every piece of logic after a render belongs in useEffect.
How did it go? Be honest — “Not yet” puts the question on your review list.
Related questions
- C-P04 · What is useEffect for, and what does the dependency array mean? — React, Next.js and frontend