Skip to content

Instantly share code, notes, and snippets.

@mrclay
Created May 5, 2020 15: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 mrclay/b9526b9e21311776070a483230e9230c to your computer and use it in GitHub Desktop.
Save mrclay/b9526b9e21311776070a483230e9230c to your computer and use it in GitHub Desktop.
usePrevious in TypeScript with init value
import { useEffect, useRef } from 'react';
export default function usePrevious<T, U>(value: T, init: U): T | U {
const ref = useRef<T | U>(init);
useEffect(() => {
ref.current = value;
});
return ref.current;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment