All files / molecule/stepper/src/hooks useRefs.js

100% Statements 16/16
100% Branches 4/4
100% Functions 5/5
100% Lines 15/15

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 23 24 25 26 27 28    1x 20x 20x   20x 116x 58x 34x   58x 58x   20x 58x 24x 24x 24x         20x        
import {useLayoutEffect, useRef, useState} from 'react'
 
const useRefs = () => {
  const refs = useRef(new Set())
  const [steps, setSteps] = useState(0)
 
  const useContextRef = node => {
    if (node === null) return
    if (refs.current.has(node)) {
      refs.current.delete(node)
    }
    refs.current.add(node)
    setSteps(refs.current.size)
  }
  const useContextUnRef = ref =>
    useLayoutEffect(
      () => () => {
        refs.current.delete(ref.current)
        setSteps(refs.current.size)
      },
      [ref]
    )
 
  return {useContextRef, useContextUnRef, steps}
}
 
export default useRefs