Created
April 13, 2023 16:45
-
-
Save pachun/1a8c08c803b004bfd14f1f18d433cbac 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 } from "react" | |
const useRefsWithKeys = <T>(): { | |
setRefForKey: (key: string) => (element: T) => void | |
getRefForKey: (key: string) => T | |
} => { | |
const ref = useRef<Record<string, T>>({}) | |
const setRefForKey = | |
(key: string) => | |
(element: T): void => { | |
ref.current[key] = element | |
} | |
const getRefForKey = (key: string): T => { | |
return ref.current[key] | |
} | |
return { | |
setRefForKey, | |
getRefForKey, | |
} | |
} | |
export default useRefsWithKeys |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment