DEV Community

[Comment from a deleted post]
Collapse
 
lucassperez profile image
Lucas Perez • Edited

You can also use useEffect as componentWillUnmount if you make it return a function! This function will be called when the component is about to unmount.

useEffect(() => {
  console.log("mounted");
  return () => {
    console.log("will unmount");
  }
}, []);
Enter fullscreen mode Exit fullscreen mode

This would console log "mounted" when component did mount and console log "will unmount" when component was about to unmount.
This way we can mimic even better the class components' life cycle methods ;)