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
reducerFunc<TState, TAction, TState>Pure reducer (state, action) => nextState. Must not be null.
initialTStateInitial 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
TStateState type managed by the reducer.
TActionAction 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
reducerFunc<TState, TAction, TState>Pure reducer (state, action) => nextState. Must not be null.
initialArgTArgArgument passed to
initon the first render. Ignored afterwards.initFunc<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
TArgType of the argument forwarded to
init.TStateState type managed by the reducer.
TActionAction type accepted by the reducer.