-
Notifications
You must be signed in to change notification settings - Fork 3
4. ReducerScope
Gabriel Brasileiro edited this page Jun 13, 2020
·
4 revisions
ReducerScope is an abstract class with Reducer interface implementing getState() method.
In your implementation you will extends the ReducerScope and implements updateTo(StateEvent) method to can call the updateState(State) in your function.
Calling updateState we will receive in lambda the current state, in this moment we can copy the object passing our new params and return him in the lambda. At this moment your view will be notified with the changes.
ReducerScope implementation checklist:
- Extend your event implementation from the Event interface;
- Extend your state event implementation from the StateEvent interface.
Example:
class PersonReducer : ReducerScope<PersonData, PersonStateEvent>(
initialState = PersonData()
) {
override fun updateTo(stateEvent: PersonStateEvent) = updateState {
when (stateEvent) {
is PersonStateEvent.UpdateName -> copy(name = stateEvent.name)
is PersonStateEvent.UpdateAge -> copy(age = stateEvent.age)
}
}
}