Created
March 22, 2023 14:17
-
-
Save DWboutin/b92588676d8a5e5b297fc3d32343b066 to your computer and use it in GitHub Desktop.
usePrevious react hook
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 { | |
const ref = useRef(value) | |
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