Skip to content

Instantly share code, notes, and snippets.

@rw3iss
Created December 1, 2021 04:44
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 rw3iss/2b919403c570ca9b9ef950598d58eea7 to your computer and use it in GitHub Desktop.
Save rw3iss/2b919403c570ca9b9ef950598d58eea7 to your computer and use it in GitHub Desktop.
useJitRef.js
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