redux-class-reducer

1.0.4 • Public • Published

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
  })
  // ...
});

Readme

Keywords

none

Package Sidebar

Install

npm i redux-class-reducer

Weekly Downloads

1

Version

1.0.4

License

ISC

Unpacked Size

8.37 kB

Total Files

17

Last publish

Collaborators

  • starvinmelon