flux-action-class
Boilerplate free class-based action creator. Following flux-standard-action spec. Built with TypeScript. Works flawlessly with JavaScript and TypeScript right out of the box.
Installation
npm i flux-action-class
Quick start
import { ActionStandard } from 'flux-action-class'
class ActionToDoIncrement extends ActionStandard {}
const reducer = (state, action) => {
switch (action.type) {
case ActionToDoIncrement.type:
}
}
In depth
ActionStandard
is an abstract class with two generics, payload and meta, set to undefined
by default. It has:
- Static
type
getter based on class' name to easily use it in your reducers - Own (non-static)
type
getter which uses the static one to comply with flux-standard-action - Own (non-static)
payload
readonly field typed as the first generic (undefined
by default) - Own (non-static)
meta
readonly field typed as the second generic (undefined
by default) - Own (non-static)
error
readonly boolean field set to true
if payload is an instance of Error
- Protected static
prefix
field which is the the first part of type
set to 'flux-action-class:' by default
Examples
Create an action with no payload and no meta
import { ActionStandard } from 'flux-action-class'
class ActionTest1 extends ActionStandard {}
new ActionTest1()
new ActionTest1('test')
Create an action with payload and no meta
import { ActionStandard } from 'flux-action-class'
class ActionTest2 extends ActionStandard<number> {}
new ActionTest2(5)
new ActionTest2()
new ActionTest2('test')
new ActionTest2(5, 'test')
Create an action with payload and meta
import { ActionStandard } from 'flux-action-class'
class ActionTest3 extends ActionStandard<number, string> {}
new ActionTest3(5, 'test')
new ActionTest3()
new ActionTest3('test')
new ActionTest3(5, 45)
Create an action with no payload and meta
import { ActionStandard } from 'flux-action-class'
class ActionTest4 extends ActionStandard<undefined, string> {}
new ActionTest4(undefined, 'test')
new ActionTest4()
new ActionTest4('test')
new ActionTest4(5, 45)
Create an error action with no meta
import { ActionStandard } from 'flux-action-class'
class ActionTest5 extends ActionStandard<Error> {}
new ActionTest5(new Error())
new ActionTest3()
new ActionTest3('test')
new ActionTest3(5, 45)
Create an error action with meta
import { ActionStandard } from 'flux-action-class'
class ActionTest6 extends ActionStandard<Error, string> {}
new ActionTest6(new Error(), 'string')
new ActionTest6()
new ActionTest6('test')
new ActionTest6(5, 45)
Customize action prefix
Sub-classing
import { ActionStandard } from 'flux-action-class'
abstract class AppBaseAction<Payload = undefined, Meta = undefined> extends ActionStandard<Payload, Meta> {
protected static readonly prefix = 'app:'
}
class ActionTest7 extends AppBaseAction {}
Using setPrefix
import { ActionStandard, setPrefix } from 'flux-action-class'
setPrefix('app:')
class ActionTest7 extends ActionStandard {}
Usage in reducers
Be aware, if you're using switch-case
based reducers, TS compiler is no longer capable of automatic union discrimination, in other words the compiler can no longer match action's type by its field type
.
Consider going with selecting a reducer from a map by key (relevant article (go to Tip 3: Switch away from switch), Redux's official documentation) or using ngrx-actions instead.
You can take a look at the discussion here