Web Development
React.js
Subjective
Sep 25, 2025
What is useEffect and its cleanup?
Detailed Explanation
useEffect handles side effects in functional components. It runs after render and can optionally clean up.\n\nBasic usage:\nuseEffect(() => {\n // Side effect\n document.title = "New Title";\n}, [dependency]); // Dependency array\n\nWith cleanup:\nuseEffect(() => {\n const timer = setInterval(() => {\n console.log("Timer tick");\n }, 1000);\n \n // Cleanup function\n return () => {\n clearInterval(timer);\n };\n}, []);\n\nDependency array:\n- [] = run once (componentDidMount)\n- [value] = run when value changes\n- no array = run on every render
Discussion (0)
No comments yet. Be the first to share your thoughts!
Share Your Thoughts