Skip to content

Instantly share code, notes, and snippets.

@dimitrinicolas
Created July 30, 2020 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimitrinicolas/4241b990d542e1e97cd40b3baba5f921 to your computer and use it in GitHub Desktop.
Save dimitrinicolas/4241b990d542e1e97cd40b3baba5f921 to your computer and use it in GitHub Desktop.
React useEffect not called on first change
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);
};
@convict-git
Copy link

This practice shouldn't be encouraged in my opinion. Let's use React declaratively and not handle specific renders imperatively.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment