useObjectState

Manage complex state objects with useObjectState.

Install:

npm i @uidotdev/usehooks

Description:

The useObjectState hook is useful for managing complex state objects. It provides a convenient way to initialize and update state values using a single hook. By using this hook, you can easily create and maintain state objects with initial values, and then update them through a flexible “handleUpdate” function. This function accepts either a callback function or an object, allowing you to merge new values into the existing state object.

Parameters

NameTypeDescription
initialValueobject(Optional) The initial state value.

Return Value

The useObjectState hook returns an array with the following elements:

IndexTypeDescription
0objectThe current state object.
1functionA function to update the state object.

Demo:

Example:

More Hooks:

Sort: