Created
August 24, 2021 11:11
-
-
Save codemile/45f6f5b4dc33f3f45408104c66511344 to your computer and use it in GitHub Desktop.
Hook that returns the previous value.
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'; | |
export function usePrevious(value) { | |
const ref = useRef(value); | |
useEffect(() => { | |
ref.current = value; | |
}, [value]); | |
return ref.current; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment