123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- export function refToCallback(ref) {
- return (newValue) => {
- if (typeof ref === 'function') {
- ref(newValue);
- }
- else if (ref) {
- ref.current = newValue;
- }
- };
- }
- const nullCallback = () => null;
- const weakMem = new WeakMap();
- const weakMemoize = (ref) => {
- const usedRef = ref || nullCallback;
- const storedRef = weakMem.get(usedRef);
- if (storedRef) {
- return storedRef;
- }
- const cb = refToCallback(usedRef);
- weakMem.set(usedRef, cb);
- return cb;
- };
- export function useRefToCallback(ref) {
- return weakMemoize(ref);
- }
|