@jeswr/use-state
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

useState

React hook with same API as useState (plus some extensions) implemented using useReducer

GitHub license npm version build Dependabot semantic-release

Why

useReducer applies dispatch calls in the order in which they are made. The native useState functionality does not.

Usage

Using the traditional useState API

import { useState } from '@jeswr/use-state'

function MyReactComponent() {
  const [counter, setCounter] = useState(0);
  useEffect(() => {
    setCounter(1);
  }, [])
  return <div>{counter}</div>
}

Using the additional modifier parameter

import { useState } from '@jeswr/use-state'

function MyReactComponent() {
  const [counter, setCounter] = useState(0, props => props.value);
  useEffect(() => {
    setCounter({ value: 1 });
  }, [])
  return <div>{counter}</div>
}

License

©2021–present Jesse Wright, MIT License.

Package Sidebar

Install

npm i @jeswr/use-state

Weekly Downloads

0

Version

1.0.3

License

MIT

Unpacked Size

5.87 kB

Total Files

5

Last publish

Collaborators

  • jeswr