Skip to content
All writing

How to use ComponentWillUnmount in a Functional Component

Replace componentWillUnmount with a useEffect cleanup function in React functional components.

#snippetsreactjavascript

Functional components have no componentWillUnmount. Return a cleanup function from useEffect instead. With an empty dependency array, the effect runs once on mount and the cleanup runs once on unmount.

useEffect(() => {
	return () => {
		// Anything in here is fired on component unmount.
	};
}, []);

Get new posts by email

New writing in your inbox. Unsubscribe anytime.