1.0.4 • Published 5 years ago
redux-class-reducer v1.0.4
redux-class-reducer
Library for defining reducers as classes with separate handlers to get rif of giant switch cases and to have some oop like looking code.
@Reducer()
class SomeReducer {
// declare which types must be handles by that method
@OfType('DELETE')
public delete() {
}
// You can pass object with type field to params also
// it will also works with ClassParamter with static field type
@OfType({ type: 'ADD' }, { order: 2 })
public add(state : IYourStateType, action : any) {
// handle aciton here
return {
state,
...action.payload
}
}
// you also can define few handlers
// all of them will be used in specific order
// you can define order with oftype options like:
@OfType('ADD', { order: 1 }) // by default order === 1
public add() {
}
// you can also pass multiple actions to listen
// order works same way as for single actions
@OfType(['MY_ACTION', { type: 'OTHER_ACTION' }, ClassWithStaticTypeField])
public multiple() {
}
}
To use that class as reducer you need to:
import { toReducer } from "redux-class-reducer";
combineReducers({
// ...
name: toReducer(new SomeReducer(), {
// pass default state
})
// ...
});