# All files / src/_hooks Buffer.tsx

100% Statements 29/29
75% Branches 3/4
100% Functions 9/9
100% Lines 17/17

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
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    1143x

    21x
    21x

    21x
    21x
    21x

    21x

    3x
    2x

    2x

    9x
    3x

    3x

    3x
    2x

    3x
    3x

     import React from 'react'

    /* TYPES */

    interface BufferConfig {
      initialValue: string
    }

    interface BufferApi {
      clear: () => void
      reset: () => void
      update: (value: string) => void
      value: string
    }

    interface Props extends BufferConfig {
      children?: React.ReactNode
    }

    interface RenderProps extends BufferConfig {
      children: (props: BufferApi) => {}
    }

    /* HOOK */

    /**
     * @deprecatedSince 9.7.0
     * @deprecated A trivial hook for storing a string. Write your own or use a hook utility lib.
     *
     * **Source:**
     * ```
      export function useBuffer({ initialValue = '' }) {
      const [value, setValue] = React.useState(initialValue)

      const clear = () => setValue('')

      const reset = () => setValue(initialValue)

      const update = (value: string) => setValue(value)

      return {
        clear,
        reset,
        update,
        value,
      }
    }
    ```
     */
    export function useBuffer({ initialValue = '' }: BufferConfig): BufferApi {
      const [value, setValue] = React.useState(initialValue)

      const clear = () => setValue('')
      const reset = () => setValue(initialValue)
      const update = (value: string) => setValue(value)

      return {
        clear,
        reset,
        update,
        value,
      }
    }

    /* RENDER PROP */

    /**
     * @deprecatedSince 9.7.0
     * @deprecated
     */
    export function Buffer({ children, ...props }: RenderProps) {
      const buffer = useBuffer(props)

      return typeof children === 'function' ? children(buffer) : children
    }

    /* CONTEXT */

    const noop = (method: string) => () =>
      console.warn(
        `Could not call \`${method}\`, please wrap your component in a \`<BufferProvider>\``
      )

    /**
     * @deprecatedSince 9.7.0
     * @deprecated
     */
    export const BufferContext = React.createContext<BufferApi>({
      value: '',
      clear: noop('clear'),
      reset: noop('reset'),
      update: noop('update'),
    })

    /**
     * @deprecatedSince 9.7.0
     * @deprecated
     */
    export function BufferProvider({ children, ...props }: Props) {
      return (
        <BufferContext.Provider value={useBuffer(props)}>
          {children}
        </BufferContext.Provider>
      )
    }

    /**
     * @deprecatedSince 9.7.0
     * @deprecated
     */
    export function useBufferContext() {
      return React.useContext(BufferContext)
    }