Created
June 14, 2019 12:04
-
-
Save donaldpipowitch/460ecacee2de48a7a965b4c08eb358d7 to your computer and use it in GitHub Desktop.
React Hook to use 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'; | |
function usePrevious<T>(value: T) { | |
const ref = useRef<T>(); | |
useEffect(() => { | |
ref.current = value; | |
}); | |
return ref.current; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment