UNPKG

556 BJavaScriptView Raw
1import * as React from 'react';
2import setRef from './setRef';
3export default function useForkRef(refA, refB) {
4 /**
5 * This will create a new function if the ref props change and are defined.
6 * This means react will call the old forkRef with `null` and the new forkRef
7 * with the ref. Cleanup naturally emerges from this behavior
8 */
9 return React.useMemo(() => {
10 if (refA == null && refB == null) {
11 return null;
12 }
13
14 return refValue => {
15 setRef(refA, refValue);
16 setRef(refB, refValue);
17 };
18 }, [refA, refB]);
19}
\No newline at end of file