Table of Contents

Method UseReducer

Namespace
Velvet
Assembly
Velvet.Docs.dll

UseReducer<TState, TAction>(Func<TState, TAction, TState>, TState)

Local state hook that derives the next state from the current state and a dispatched action.

public static (TState state, Action<TAction> dispatch) UseReducer<TState, TAction>(Func<TState, TAction, TState> reducer, TState initial)

Parameters

reducer Func<TState, TAction, TState>

Pure reducer (state, action) => nextState. Must not be null.

initial TState

Initial state used on the first render. Ignored on subsequent renders.

Returns

(TState state, Action<TAction> dispatch)

2-tuple:

  • state: current state value.
  • dispatch: action dispatcher that schedules a re-render with the next state.

Type Parameters

TState

State type managed by the reducer.

TAction

Action type accepted by the reducer.

UseReducer<TArg, TState, TAction>(Func<TState, TAction, TState>, TArg, Func<TArg, TState>)

Lazy-initialized variant taking an init function to compute the initial state. init is invoked exactly once with initialArg on the first render to produce the initial state, then never called again. Use this to defer expensive initialization (large arrays, dictionaries, etc.) so that it does not run on every render.

public static (TState state, Action<TAction> dispatch) UseReducer<TArg, TState, TAction>(Func<TState, TAction, TState> reducer, TArg initialArg, Func<TArg, TState> init)

Parameters

reducer Func<TState, TAction, TState>

Pure reducer (state, action) => nextState. Must not be null.

initialArg TArg

Argument passed to init on the first render. Ignored afterwards.

init Func<TArg, TState>

Initializer that computes the initial state from initialArg. Must not be null.

Returns

(TState state, Action<TAction> dispatch)

2-tuple:

  • state: current state value.
  • dispatch: action dispatcher that schedules a re-render with the next state.

Type Parameters

TArg

Type of the argument forwarded to init.

TState

State type managed by the reducer.

TAction

Action type accepted by the reducer.