0.1.1 • Published 2 years ago

use-ultimate-hooks v0.1.1

Weekly downloads
-
License
GPL-3.0
Repository
github
Last release
2 years ago

Use Ultimate Hooks

This is a set of developer friendly custom React hooks, that will make your life easier and your code cleaner.

Installation

npm install use-ultimate-hooks

Usage

import { IUseReducedStateReturn, useReducedState } from 'use-ultimate-hooks'
interface IState {
  count: number
  name: string
}
export default function App() {
  const {state, updateState} = useReducedState<IState>({
    count: 0,
    name: 'John'
  })

  return (
    <>
      <div>{state.count}</div>
      <div>{state.name}</div>
      <button onClick={() => updateState({count: state.count + 1})>Increment</button>
      <button onClick={() => updateState({name: 'Jane'})>Change name</button>
    </>
  )
}

Hooks

useReducedState

Handle complex state with ease.

  • It uses the reducer pattern to save and perform actions on the state such as update a particular property of the state.
  • It is a wrapper around React's useReducer hook, that allows you to update the state by passing an object with the properties you want to update.
  • It uses generics to define the type of the state, which makes it easier to use.
interface IState {
  user?: {
    name?: string
    email?: string
  }
  token?: string
  orgs?: string[]
}
const { state, updateState } = useReducedState<IState>(initialState)

Returns an Object

NameTypeDescription
stateIStateThe current state
updateState(state: IState) => voidA function to update the state
patchState(state: Partial) => voidA function to update the state with a patch function
clearState() => voidA function to clear the state
resetState() => voidA function to reset the state to the initial value
updateFieldValue(field: keyof IState, value: any) => voidA function to update a specific field in the state
removeFieldValue(field: keyof IState) => voidA function to remove a specific field in the state
resetFieldValue(field: keyof IState) => voidA function to reset a specific field in the state

useLocalStorage

Save and retrieve data from the local storage smartly with ease.

  • It uses generics to define the type of the data, which makes it easier to use.
  • It stores the data in the local storage, and updates the state when the data changes in the local storage.
  • It adapts to the data type, and stores it in the local storage as a string, and parses it when retrieving it.
const [value, setValue] = useLocalStorage<Record<string, any>>(key, initialValue)

Returns an Array

NameTypeDescription
valueTThe current value
setValue(value: T) => voidA function to update the value
0.1.1

2 years ago

0.1.0

2 years ago