|
1
|
import { MutableRefObject, Ref, RefCallback } from "react" |
|
2
|
|
|
3
|
// Utility function to set multiple refs |
|
4
|
function setRef<T>(ref: Ref<T> | undefined, value: T) { |
|
5
|
if (typeof ref === "function") { |
|
6
|
ref(value) |
|
7
|
} else if (ref && "current" in ref) { |
|
8
|
;(ref as MutableRefObject<T>).current = value |
|
9
|
} |
|
10
|
} |
|
11
|
|
|
12
|
// Combining multiple refs into one |
|
13
|
export const useCombinedRefs = <T,>( |
|
14
|
...refs: (Ref<T> | undefined)[] |
|
15
|
): RefCallback<T> => { |
|
16
|
return (value: T) => { |
|
17
|
refs.forEach((ref) => setRef(ref, value)) |
|
18
|
} |
|
19
|
} |