useSignalState

Tags:

A React Hook similar to createSignal from Solid, which allows reading the state value via the state() method, avoiding closure issues.

Scenes

  • Preventing closure issues: Effectively solves outdated closure issues that might occur with traditional useState in closure scenarios.
  • ...

Demo

Source

Usage

const [state, setState] = useSignalState(0) console.log(state()) // Call the function to read the state value, ensuring it's always up to date setState(1) // Set a new value

Source

Click links below to view source on GitHub.

API

const [state, setState] = useSignalState(initialState)

InitialState

The same as React.useState.

Options

Refer to useSafeState#options.

Returns

export type UseSignalStateReturns = [() => T, ReactSetState<T>]