Documentation
useCurrentState

useCurrentState

The useCurrentState hook combines the functionality of React's state and ref hooks. It provides state management along with a mutable ref that always contains the most up-to-date value of the state. This is particularly useful in scenarios where you need synchronous access to the current state value within asynchronous functions or event handlers.

Usage

First, you need to import the useCurrentState hook from the kitchn package.

import { useCurrentState } from "kitchn";

Example

Here is an example of how to use the useCurrentState hook in a component:

Parameters

The useCurrentState hook accepts the following parameter:

NameTypeDescription
initialStateS | () => SThe initial state value, or a function that returns the initial state.

Return Value

The useCurrentState hook returns a tuple with the following elements:

NameTypeDescription
stateSThe current state value.
setStateReact.Dispatch<React.SetStateAction<S>>A function to update the state, similar to setState in React.
refReact.MutableRefObject<S>A ref object containing the most up-to-date state value.