You cannot use any of the existing lifecycle methods (componentDidMount, componentDidUpdate, componentWillUnmount etc.) in a hook. They can only be used in class components. And with Hooks you can only use in functional components.
You can think of useEffect Hook as componentDidMount, componentDidUpdate, and componentWillUnmount combined.
Code inside componentDidMount run once when the component is mounted. useEffect hook equivalent for this behaviour is
useEffect(() => {
// Your code here
}, []);
Without the second parameter the useEffect hook will be called on every render (like componentDidUpdate) of the component which can be dangerous:
useEffect(() => {
// Your code here
});
Hook equivalent of componentWillUnmount() code will be as follows
useEffect(() => {
window.addEventListener('mousemove', () => {});
// returned function will be called on component unmount
return () => {
window.removeEventListener('mousemove', () => {})
}
}, [])