useEffect hook in react

useEffect hook can be used to implement below lifecycle methods of component.
  • componentDidMount() - use useEffect and pass empty array as second argument
  • componentDidUpdate() -
    
                    const mounted = useRef();
                    useEffect(() => {
                        if (!mounted.current) {
                            // componentDidMount code
                            mounted.current = true;
                        } else {
                            //componentDidUpdate code goes here
                        }
                    });
                    
  • shouldComponentUpdate() - pass dependency array as second argument
  • componentWillUnmount() - return a function from useEffect


Complex problems, Simple Solutions