Created
July 10, 2020 18:29
-
-
Save ikibalnyi/0ee284231ee91fe46b123caf585da944 to your computer and use it in GitHub Desktop.
Hook to keep persistent reference to a variable
This file contains hidden or 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
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