Skip to content

Instantly share code, notes, and snippets.

@sstur
Created July 10, 2022 21:11
Show Gist options
  • Save sstur/7dfde04fb34afc39ffdb9320dbeb67c3 to your computer and use it in GitHub Desktop.
Save sstur/7dfde04fb34afc39ffdb9320dbeb67c3 to your computer and use it in GitHub Desktop.
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