Skip to content

Instantly share code, notes, and snippets.

@heytulsiprasad
Last active October 27, 2021 13:05
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 heytulsiprasad/34000e3b9e5ec22864a3122b5c14129c to your computer and use it in GitHub Desktop.
Save heytulsiprasad/34000e3b9e5ec22864a3122b5c14129c to your computer and use it in GitHub Desktop.
Custom hook that doesn't run on initial render
import { useRef, useEffect } from "react";
const useNonInitialEffect = (effect, deps = []) => {
const initialRender = useRef(true);
useEffect(() => {
let effectReturns = () => {};
if (initialRender.current) {
initialRender.current = false;
} else {
effectReturns = effect();
}
if (effectReturns && typeof effectReturns === "function") {
return effectReturns;
}
}, deps);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment