This repository has been archived on 2025-07-07. You can view files and clone it, but cannot push or open issues or pull requests.
Files
2025-01-11 09:54:09 +03:00

49 lines
1.2 KiB
JavaScript

/**
* Unmemoized version of {@link useRefToCallback}
* @see {@link useRefToCallback}
* @param ref
*/
export function refToCallback(ref) {
return (newValue) => {
if (typeof ref === 'function') {
ref(newValue);
}
else if (ref) {
ref.current = newValue;
}
};
}
const nullCallback = () => null;
// lets maintain a weak ref to, well, ref :)
// not using `kashe` to keep this package small
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;
};
/**
* Transforms a given `ref` into `callback`.
*
* To transform `callback` into ref use {@link useCallbackRef|useCallbackRef(undefined, callback)}
*
* @param {ReactRef} ref
* @returns {Function}
*
* @see https://github.com/theKashey/use-callback-ref#reftocallback
*
* @example
* const ref = useRef(0);
* const setRef = useRefToCallback(ref);
* 👉 setRef(10);
* ✅ ref.current === 10
*/
export function useRefToCallback(ref) {
return weakMemoize(ref);
}