# All files / src/_utils mergeRefs.tsx

100% Statements 10/10
100% Branches 3/3
100% Functions 4/4
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22

    86x
    15949x
    2530x
    13419x
    7480x

    18295x
    4158x
    15946x

     import React from 'react'

    type RefCallback<T> = (newValue: T | null) => void

    type RefObject<T> = React.MutableRefObject<T | null>

    type ReactRef<T> = RefCallback<T> | RefObject<T> | null

    export function assignRef<T>(ref: ReactRef<T>, value: T | null) {
      if (typeof ref === 'function') {
        ref(value)
      } else if (ref) {
        ref.current = value
      }
    }

    export function mergeRefs<T>(...refs: ReactRef<T>[]): RefCallback<T> {
      return (value) => {
        refs.forEach((ref) => assignRef(ref, value))
      }
    }