Skip to content

Instantly share code, notes, and snippets.

@deviationist
Last active March 11, 2024 15:45
Show Gist options
  • Save deviationist/b1005c39c8fd270606648053d48a55fe to your computer and use it in GitHub Desktop.
Save deviationist/b1005c39c8fd270606648053d48a55fe to your computer and use it in GitHub Desktop.
React's useEffect, but it does not run on initial render. Now with TypeScript-support.
/* eslint-disable react-hooks/exhaustive-deps */
import { useEffect, useRef, DependencyList } from 'react';
export const useEffectNonInit = (effect: Function, deps: DependencyList = []) => {
const isInitial = useRef<boolean>(true);
useEffect(() => {
if (isInitial.current) {
isInitial.current = false;
return;
}
effect();
}, deps);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment