1.0.8 • Published 1 year ago

use-state-async v1.0.8

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

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.
1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago