Created
February 8, 2024 23:49
-
-
Save akullpp/71dfaf650f2d2bbe4de3ee88fe5b74cf to your computer and use it in GitHub Desktop.
Use previous value hook for React
This file contains 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'; | |
const usePrevious = (value) => { | |
const ref = useRef(); | |
useEffect(() => { | |
ref.current = value; | |
}, [value]); | |
return ref.current; | |
} | |
export { usePrevious } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment