Skip to content

Instantly share code, notes, and snippets.

@milesegan
Created April 22, 2019 07:39
Show Gist options
  • Save milesegan/ca57076fbe370dfd59ec23adcd98c885 to your computer and use it in GitHub Desktop.
Save milesegan/ca57076fbe370dfd59ec23adcd98c885 to your computer and use it in GitHub Desktop.
React hooks utilities.
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