# All files / src/_hooks useUpdateEffect.ts

100% Statements 12/12
100% Branches 2/2
100% Functions 3/3
100% Lines 11/11

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
    275x

    35x

    35x
    9x

    9x

    26x

    5x
    35x

    35x
    17x
    8x

     import React from 'react'

    function useFirstMountState(): boolean {
      const isFirst = React.useRef(true)

      if (isFirst.current) {
        isFirst.current = false

        return true
      }

      return isFirst.current
    }

    /**
     * @description useUpdateEffect can be used to perform a side effect when the component updates, except when it's first mounted
     */
    export const useUpdateEffect: typeof React.useEffect = (effect, deps) => {
      const isFirstMount = useFirstMountState()

      React.useEffect(() => {
        if (!isFirstMount) {
          return effect()
        }
      }, deps)
    }