Last active
September 22, 2025 09:23
-
-
Save jeongtae/896be1318c4b74b726262ba8d269aa9f to your computer and use it in GitHub Desktop.
usePrevious React hook in Typescript
This file contains hidden or 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'; | |
| function usePrevious<T>(value: T): T | undefined { | |
| const ref = useRef<T>(); | |
| useEffect(() => { | |
| ref.current = value; | |
| }, [value]); | |
| return ref.current; | |
| } | |
| export default usePrevious; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice! Thank you so much!