-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
useMergedRef.mjs
35 lines (31 loc) · 1003 Bytes
/
useMergedRef.mjs
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
// @ts-check
import React from "react";
/**
* A React hook that merges multiple React refs into one to use on an element.
* @see [GitHub comment](https://github.com/facebook/react/issues/13029#issuecomment-522632038).
* @template T
* @param {Array<
* | React.MutableRefObject<T | null>
* | React.RefCallback<T>
* | null
* | undefined
* >} refs React refs. `undefined` is allowed to make it easy to use refs from
* optional React component props.
* @returns {React.MutableRefObject<T | null>} Merged React ref.
*/
export default function useMergedRef(refs) {
const mergedRef = React.useRef(/** @type {T | null} */ (null));
React.useEffect(
() => {
for (const ref of refs) {
// Skip an undefined or null ref.
if (!ref) continue;
if (typeof ref === "function") ref(mergedRef.current);
else ref.current = mergedRef.current;
}
},
// eslint-disable-next-line react-hooks/exhaustive-deps
refs
);
return mergedRef;
}