Mid
From PDF
React
React.js
How do you mimic componentDidMount and componentWillUnmount with hooks?
✅ componentDidMount:
useEffect(() => {
console.log("Component mounted");
}, []); // Empty array = run once on mount
✅ componentWillUnmount:
useEffect(() => {
const id = setInterval(() => console.log("tick"), 1000);
return () => {
clearInterval(id); // Cleanup
console.log("Component unmounted");
};
}, []);
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png