Skip to content

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.

When we call updateState we will receive in lambda the current state, in this moment we can copy the object and return him in the lambda expected return.

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)
        }
    }
}

Code Sample

Clone this wiki locally