-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathuseProxyProps.ts
More file actions
37 lines (33 loc) · 1.26 KB
/
useProxyProps.ts
File metadata and controls
37 lines (33 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { useRef } from "react";
/**
* `useProxyProps` can be used to maintain a reference to an updated props, without that change triggering
* another re-render. This is useful if, for example, some props should be passed through a parent component to
* a child function that contains code that needs the most up-to-date-reference, without causing the parent to
* rerender.
*/
export function useProxyProps<T extends Record<PropertyKey, unknown>>(
props: T,
): T {
const proxyRef = useRef(
new Proxy<T>({} as T, {
get(target: T, prop: PropertyKey, receiver) {
/**
*
* If target[prop] is a function, return a function that accesses
* this function off the target object. We can mutate the target with
* new copies of this function without having to re-render the
* SDK components to pass new callbacks.
*
* */
if (typeof target[prop] === "function") {
return (...args: unknown[]) =>
// eslint-disable-next-line @typescript-eslint/ban-types
(target[prop] as Function)(...args);
}
return Reflect.get(target, prop, receiver);
},
}),
);
proxyRef.current = Object.assign(proxyRef.current, props);
return proxyRef.current;
}