Skip to content

Instantly share code, notes, and snippets.

@js2me
Created November 29, 2021 08:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save js2me/20158829bb6ef0f885d7096796f53408 to your computer and use it in GitHub Desktop.
Save js2me/20158829bb6ef0f885d7096796f53408 to your computer and use it in GitHub Desktop.
use sync ref
import { MutableRefObject, useEffect, useRef } from 'react'
export const useSyncRef = <T>(value: T): MutableRefObject<T> => {
const ref = useRef<T>(value)
useEffect(() => {
ref.current = value
}, [value])
return ref
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment