Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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;
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #react #useeffect #render
ADD COMMENT
Topic
Name
8+1 =