Last active
May 6, 2020 10:22
-
-
Save mic-css/3230b49d1337e64f66135f91f6294c47 to your computer and use it in GitHub Desktop.
Custom hook that keeps track of the previous value of an object or primitive, useful for comparing props between renders
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<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