Created
April 22, 2019 07:39
-
-
Save milesegan/ca57076fbe370dfd59ec23adcd98c885 to your computer and use it in GitHub Desktop.
React hooks utilities.
This file contains 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 { useEffect, useRef } from "react"; | |
// Runs only on update, not on initial render, unlike useEffect. | |
// Handy replacement for componentDidUpdate(). | |
export function useUpdateEffect( | |
effect: React.EffectCallback, | |
deps?: React.DependencyList | |
): void { | |
const didMountRef = useRef(false); | |
useEffect(() => { | |
if (didMountRef.current) { | |
effect(); | |
} else { | |
didMountRef.current = true; | |
} | |
}, deps); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment