Tiny library for using js/ts classes as action creators
// Just extends action class and set static
// field to instance field on initialization
@Action()
class MyAction {
/**
* Static type field which will be converted to property on new MyAction()
*/
static type = 'type'
}
// somewhere in reducer file
// if typescriot there you can:
// action: TypedAction<T>
const myReducer = (state, action) => {
switch(action.type) {
case MyAction.type: {
// do something
}
// ...
}
}
Also because redux does't support nothing but object in dispatch by default there is reduxActionTypeMiddleware - just converts class instances to plain object to overpass it
import { reduxActionTypeMiddleware } from 'redux-action-type'
// apply mw to your store to use classes
createStore(reducer, applyMiddleware(reduxActionTypeMiddleware))
Also for typescript. Lib has redefinition of redux dispatch that allows to pass anything in dispatch ( because with cannot track fields added dynamically so it's only way to overpass it )