use-merged-ref
Merges multiple refs objects or functions into one ref callback
Import
Source
Docs
Package
Usage
use-merged-ref
accepts any number of refs and returns a function that should be passed to the ref
prop.
Use this hook when you need to use more than one ref on a single dom node, for example,
when you want to use use-click-outside and use-focus-trap hooks and also get a ref for yourself:
mergeRefs function
use-merged-ref
hooks memoizes refs with useCallback
hook, but in some cases
memoizing is not a valid strategy, for example, when you are working with a list
of dynamic components React will complain that different number of hooks was called
across two renders. To fix that issue, use mergeRefs
function instead:
mergeRefs
works the same way as use-merged-ref
, but does not use hooks internally.
Use it only when you cannot use use-merged-ref
. Note that mergeRefs
will not work
correctly with use-focus-trap hook, you are required to
use use-merged-ref
with it.
assignRef function
assignRef
function can be used to assign refs that are not memoized with useCallback
.
It is usually used to assign refs that do not reference elements: