Skip to content

useReducerGetReset ​

Custom useReducer with get and reset state functions.

Demo ​

INFO

The implementation is like that useStateGetReset but built on useReducer.

Please visit useStateGetReset example to see how it works.

Types ​

UseReducerGetResetProps ​

  • @template R - The reducer type, extending Reducer<any, any>.

Parameters accepted by useReducerGetReset.

PropertyTypeRequiredDescription
reducerR✓The reducer function (state, action) => state that computes the next state given the current state and a dispatched action.
initialStateReducerState<R>✓The initial reducer state value, passed directly to useReducer.
initializer(init: ReducerState<R>) => ReducerState<R>An optional initializer function that receives initialState and returns the actual initial state. Useful for lazily computing the initial value or extracting it from a larger object.

UseReducerGetResetResult ​

  • @template R - The reducer type.

Return value of useReducerGetReset.

IndexTypeDescription
[0]ReducerState<R>The current reducer state, reactive — triggers a re-render when updated.
[1]Dispatch<ReducerAction<R>>The stable dispatch function used to send actions to the reducer.
[2]() => ReducerState<R>A stable getter that returns the current reducer state synchronously without causing a re-render. Useful for reading state inside callbacks without adding it as a dependency.
[3]() => voidResets the reducer state back to the original initialState (after applying initializer if provided), dispatching a reset internally and triggering a re-render.

Released under the MIT License.