2.0.1 • Published 2 years ago

@kyleshevlin/use-common v2.0.1

Weekly downloads
5
License
MIT
Repository
github
Last release
2 years ago

use-common

Just some common custom React hooks so I don't have to keep writing them over and over.

  • useBool
  • useCounter
  • useNumber
  • usePrevious
  • useString

More to come.

Getting started

Install the package:

npm install --save @kyleshevlin/use-common

And use the hooks

import { useBool } from '@kyleshevlin/use-common'

function Toggle() {
  const [isOpen, { toggle }] = useBool()

  return (
    <button type="button" onClick={toggle}>
      {String(isOpen)}
    </button>
  )
}

Philosophy

By creating a library of small hooks with defined state handlers, I can quickly compose more complex custom hooks through composition. For example, I can make a useTextInput hook with the following composition:

function useTextInput(initialState = '') {
  const [state, handlers] = useString(initialState)
  const handleChange = React.useCallback(e => {
    handlers.update(e.target.value)
  })

  return [state, { ...handlers, handleChange }]
}

// And in a component
const [value, { handleChange }] = useTextInput()

API

These hooks follow a common pattern of returning a tuple of state and handlers for that state. When composing functionality with these hooks, rename the state to what a name that works best for your use case with array destructuring, and rename the individual handlers with object destructuring assignment.

useBool

This hook manages a Boolean state.

Arguments

ArgumentTypeDefaultDescription
initialStatebooleanfalseThe initial state of the hook

Handlers

NameTypeDescription
off() => voidSets the state to false
on() => voidSets the state to true
reset() => voidSets the state to the initialState
toggle() => voidFlips the state

useString

This hook manages a String state and ensures it remains a String.

Arguments

ArgumentTypeDefaultDescription
initialStatestring''The initial state of the hook

Handlers

NameTypeDescription
updatestring | ((currentState: string) => string)Updates the state to the new string
reset() => voidSets the state to the initialState
empty() => voidSets the state to an empty string

useNumber

This hook manages a Number state and ensures it remains a Number.

Arguments

ArgumentTypeDefaultDescription
initialStatenumber0The initial state of the hook

Handlers

NameTypeDescription
updatenumber | ((currentState: number) => number)Updates the state to the new number
reset() => voidSets the state to the initialState
zero() => voidSets the state to 0

useCounter

This hook builds a counter on top of useNumber.

Arguments

ArgumentTypeDefaultDescription
initialStatenumber0The initial state of the hook
stepnumber1The amount of change used by the inc and dec handlers

Handlers

NameTypeDescription
inc() => voidIncreases the state by the step
dec() => voidDecreases the state by the step
reset() => voidSets the state to the initialState
zero() => voidSets the state to 0

usePrevious

This hook manages a value's previous state. It returns the value from the previous render.

Arguments

ArgumentTypeDescription
valueTThe value the hook will track
2.0.1

2 years ago

2.0.0

3 years ago

1.3.0

3 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago