Skip to content

Instantly share code, notes, and snippets.

@jeongtae
Last active September 22, 2025 09:23
Show Gist options
  • Save jeongtae/896be1318c4b74b726262ba8d269aa9f to your computer and use it in GitHub Desktop.
Save jeongtae/896be1318c4b74b726262ba8d269aa9f to your computer and use it in GitHub Desktop.
usePrevious React hook in Typescript
import { useEffect, useRef } from 'react';
function usePrevious<T>(value: T): T | undefined {
const ref = useRef<T>();
useEffect(() => {
ref.current = value;
}, [value]);
return ref.current;
}
export default usePrevious;
@riteshjagga
Copy link

Nice! Thank you so much!

@lucaoskaique
Copy link

nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment