Created
July 30, 2020 08:39
-
-
Save dimitrinicolas/4241b990d542e1e97cd40b3baba5f921 to your computer and use it in GitHub Desktop.
React useEffect not called on first change
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useRef, useEffect, EffectCallback, DependencyList } from 'react'; | |
/** | |
* @param {EffectCallback} effect | |
* @param {DependencyList} [deps] | |
*/ | |
export const useEffectNotFirst = (effect, deps) => { | |
const firstUpdate = useRef(true); | |
useEffect(() => { | |
if (firstUpdate.current) { | |
firstUpdate.current = false; | |
return; | |
} | |
effect(); | |
}, deps); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This practice shouldn't be encouraged in my opinion. Let's use React declaratively and not handle specific renders imperatively.