Answers for "react useeffect not on first render"

3

react useeffect not on first render

//This is a way to build this effect as a custom hook
import React, { useEffect, useRef } from 'react';

const useDidMountEffect = (func, deps) => {
    const didMount = useRef(false);

    useEffect(() => {
        if (didMount.current) func();
        else didMount.current = true;
    }, deps);
}

export default useDidMountEffect;
Posted by: Guest on June-27-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language