Created
December 1, 2021 04:44
-
-
Save rw3iss/2b919403c570ca9b9ef950598d58eea7 to your computer and use it in GitHub Desktop.
useJitRef.js
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
const none = {}; | |
export function useJitRef(init) { | |
const value = useRef(none); | |
const ref = useLazyRef(() => ({ | |
get current() { | |
if (value.current === none) { | |
value.current = init(); | |
} | |
return value.current; | |
}, | |
set current(v) { | |
value.current = v; | |
} | |
})); | |
return ref; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment