Batool Nassar
Batool Nassar

Reputation: 53

How can I let the useEffect run only when its parameter change?

The useEffect run in the first and when the parameter change is run another one, I don't want the use effect run until the parameter change only. How?

Upvotes: 1

Views: 1325

Answers (1)

Sharpek
Sharpek

Reputation: 676

You can use hook like this to check it's first mount/render

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

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

Next you can check result ot useDidMountEffect

Upvotes: 1

Related Questions