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
// Creates action with no payload, no meta and type 'flux-action-class:ActionToDoIncrement'
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 totrue
if payload is an instance ofError
- Protected static
prefix
field which is the the first part oftype
set to 'flux-action-class:' by default
Examples
Create an action with no payload and no meta
// Creates action with no payload, no meta, type 'flux-action-class:ActionTest1' and error = false // TS compiler expects no arguments provided to the constructornew ActionTest1 // Correctnew ActionTest1'test' // Failure
Create an action with payload and no meta
// Creates action with payload of type number, no meta, type 'flux-action-class:ActionTest2' and error = false // TS compiler expects one argument provided to the constructornew ActionTest25 // Correctnew ActionTest2 // Failurenew ActionTest2'test' // Failurenew ActionTest25, 'test' // Failure
Create an action with payload and meta
// Creates action with payload of type number, meta of type string, type 'flux-action-class:ActionTest3' and error = false // TS compiler expects two arguments provided to the constructornew ActionTest35, 'test' // Correctnew ActionTest3 // Failurenew ActionTest3'test' // Failurenew ActionTest35, 45 // Failure
Create an action with no payload and meta
// Creates action with no payload, meta of type string, type 'flux-action-class:ActionTest4' and error = false // TS compiler expects two arguments provided to the constructornew ActionTest4undefined, 'test' // Correctnew ActionTest4 // Failurenew ActionTest4'test' // Failurenew ActionTest45, 45 // Failure
Create an error action with no meta
// Creates action with error payload, no meta, type 'flux-action-class:ActionTest5' and error = true // TS compiler expects one argument provided to the constructornew ActionTest5new Error // Correctnew ActionTest3 // Failurenew ActionTest3'test' // Failurenew ActionTest35, 45 // Failure
Create an error action with meta
// Creates action with error payload, meta of type string, type 'flux-action-class:ActionTest6' and error = true // TS compiler expects one argument provided to the constructornew ActionTest6new Error, 'string' // Correctnew ActionTest6 // Failurenew ActionTest6'test' // Failurenew ActionTest65, 45 // Failure
Customize action prefix
Sub-classing
// Creates action with no payload, no meta, type 'app:ActionTest7' and error = false
setPrefix
Using // Add it only once and it changes default prefix for all actionssetPrefix'app:' // Creates action with no payload, no meta, type 'app:ActionTest7' and error = false
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