use-state-async
TypeScript icon, indicating that this package has built-in type declarations

1.0.8 • Public • Published

useStateAsync Function

The useStateAsync function is a custom React hook that allows asynchronous updates to the state.

Usage

const { value, setValue, updateValue } = useStateAsync({ name: '', age: 0 });

const handleNameChange = (event) => {
  updateValue('name', event.target.value, (prevValue, nextValue) => {
    console.log(`The name is now ${nextValue.name}`);
  });
};

const handleAgeChange = (event) => {
  updateValue({ age: event.target.value }, ({prevValue, nextValue}) => {
    console.log(`The age is now ${nextValue.age}`);
  });
};

Parameters

The values parameter is an optional object that defines the initial state of the component.

Return value

The useStateAsync function returns an object with three properties:

  • value The current state of the component.
  • setValue A function that sets the state to a new value.
  • updateValue A function that allows asynchronous updates to the state.

Asynchronous updates

The updateValue function allows you to update the state asynchronously. It takes three parameters:

  • fieldNameOrValues: Either a string that represents the name of the field to update, or an object that represents the fields and their values to update.
  • valueOrCallback (optional): If fieldNameOrValues is a string, this parameter represents the new value of the field. If fieldNameOrValues is an object, this parameter is a callback function that will be called after the state has been updated.
  • callback (optional): If fieldNameOrValues is an object, this parameter represents a callback function that will be called after the state has been updated.

Package Sidebar

Install

npm i use-state-async

Weekly Downloads

3

Version

1.0.8

License

ISC

Unpacked Size

4.81 kB

Total Files

4

Last publish

Collaborators

  • hoangdaicntt