Skip to content

Instantly share code, notes, and snippets.

@ikibalnyi
Created July 10, 2020 18:29
Show Gist options
  • Save ikibalnyi/0ee284231ee91fe46b123caf585da944 to your computer and use it in GitHub Desktop.
Save ikibalnyi/0ee284231ee91fe46b123caf585da944 to your computer and use it in GitHub Desktop.
Hook to keep persistent reference to a variable
export const useProxyRef = <T extends object>(value: T) => {
const ref = useRef(value);
ref.current = value;
return useMemo(
() =>
new Proxy(
ref.current,
new Proxy(Reflect, {
get(target, p, receiver) {
return (_: any, ...args: any[]) =>
Reflect.get(target, p, receiver)(ref.current, ...args);
},
}) as any,
) as T,
[],
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment