UseReducer<T> class
A ReactterHook that manages state using reducer method.
UseReducer accepts a reducer method and returns the current state paired with a dispatch method. (If you’re familiar with Redux, you already know how this works.)
Contains a value of type T which represents the current state.
When value is different to previous state,
UseReducer execute update to notify context of ReactterContext
that has changed and in turn executes onWillUpdate and onDidUpdate.
Example:
class Store {
final int count;
Store({this.count = 0});
}
Store _reducer(Store state, ReactterAction<String, int?> action) {
switch (action.type) {
case 'increment':
return Store(count: state.count + (action.payload ?? 1));
case 'decrement':
return Store(count: state.count + (action.payload ?? 1));
default:
throw UnimplementedError();
}
}
class AppContext extends ReactterContext {
late final state = UseReducer(_reducer, Store(count: 0), this);
AppContext() {
print("count: ${state.value.count}"); // count: 0;
state.dispatch(ReactterAction(type: 'increment', payload: 2));
print("count: ${state.value.count}"); // count: 2;
state.dispatch(ReactterAction(type: 'decrement'));
print("count: ${state.value.count}"); // count: 1;
}
}
See also:
- ReactterContext, a context that contains any logic and allowed react when any change the ReactterHook.
- Inheritance
-
- Object
- ReactterHookManager
- ReactterHook
- UseReducer
Constructors
-
UseReducer(Reducer<
T> reducer, T initialState, [ReactterHookManager? context])
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
-
reducer
→ Reducer<
T> -
Calculates a new state with state(
T) and action(ReactterAction) given.final - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- value → T
-
no setter
Methods
-
dispatch<
A extends ReactterAction> (A action) → dynamic - Receives a ReactterAction and sends it to reducer method for resolved
-
dispose(
) → void -
Called when this object is removed
inherited
-
listenHooks(
List< ReactterHook> hooks) → void -
Suscribes to all
hooksgiven.inherited -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
refresh(
) → void -
Forces update and notifies to listeners that it did update
inherited
-
toString(
) → String -
A string representation of this object.
inherited
-
update(
[covariant Function? callback]) → void -
Executes
fnUpdate, and notify the listeners about to update.inherited -
updateAsync(
[covariant Function? callback]) → Future< void> -
Executes
fnUpdate, and notify the listeners about to update as async way.inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited