Created
May 5, 2020 15:05
-
-
Save mrclay/b9526b9e21311776070a483230e9230c to your computer and use it in GitHub Desktop.
usePrevious in TypeScript with init value
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'; | |
export default function usePrevious<T, U>(value: T, init: U): T | U { | |
const ref = useRef<T | U>(init); | |
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