Created
July 10, 2022 21:11
-
-
Save sstur/7dfde04fb34afc39ffdb9320dbeb67c3 to your computer and use it in GitHub Desktop.
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 { useRef, useEffect, useCallback } from 'react'; | |
export function useGetLatestValue<T>(value: T): () => T { | |
const valueRef = useRef(value); | |
useEffect(() => { | |
valueRef.current = value; | |
}, [value]); | |
return useCallback(() => valueRef.current, []); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment