Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Boilerplate free class-based action creator. Following flux-standard-action spec. Built with TypeScript. Works with redux and ngrx.

License

NotificationsYou must be signed in to change notification settings

fxlrnrpt/flux-action-class

Repository files navigation

Boilerplate free class-based action creator. Followingflux-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'classActionToDoIncrementextendsActionStandard{}// Creates action with no payload, no meta and type 'flux-action-class:ActionToDoIncrement'constreducer=(state,action)=>{switch(action.type){caseActionToDoIncrement.type:// Do stuff}}

In depth

ActionStandard is an abstract class with two generics, payload and meta, set toundefined by default. It has:

  • Statictype getter based on class' name to easily use it in your reducers
  • Own (non-static)type getter which uses the static one to comply withflux-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 staticprefix 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

import{ActionStandard}from'flux-action-class'classActionTest1extendsActionStandard{}// Creates action with no payload, no meta, type 'flux-action-class:ActionTest1' and error = false// TS compiler expects no arguments provided to the constructornewActionTest1()// CorrectnewActionTest1('test')// Failure

Create an action with payload and no meta

import{ActionStandard}from'flux-action-class'classActionTest2extendsActionStandard<number>{}// 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 constructornewActionTest2(5)// CorrectnewActionTest2()// FailurenewActionTest2('test')// FailurenewActionTest2(5,'test')// Failure

Create an action with payload and meta

import{ActionStandard}from'flux-action-class'classActionTest3extendsActionStandard<number,string>{}// 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 constructornewActionTest3(5,'test')// CorrectnewActionTest3()// FailurenewActionTest3('test')// FailurenewActionTest3(5,45)// Failure

Create an action with no payload and meta

import{ActionStandard}from'flux-action-class'classActionTest4extendsActionStandard<undefined,string>{}// 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 constructornewActionTest4(undefined,'test')// CorrectnewActionTest4()// FailurenewActionTest4('test')// FailurenewActionTest4(5,45)// Failure

Create an error action with no meta

import{ActionStandard}from'flux-action-class'classActionTest5extendsActionStandard<Error>{}// Creates action with error payload, no meta, type 'flux-action-class:ActionTest5' and error = true// TS compiler expects one argument provided to the constructornewActionTest5(newError())// CorrectnewActionTest3()// FailurenewActionTest3('test')// FailurenewActionTest3(5,45)// Failure

Create an error action with meta

import{ActionStandard}from'flux-action-class'classActionTest6extendsActionStandard<Error,string>{}// 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 constructornewActionTest6(newError(),'string')// CorrectnewActionTest6()// FailurenewActionTest6('test')// FailurenewActionTest6(5,45)// Failure

Customize action prefix

Sub-classing

import{ActionStandard}from'flux-action-class'abstractclassAppBaseAction<Payload=undefined,Meta=undefined>extendsActionStandard<Payload,Meta>{protectedstaticreadonlyprefix='app:'}classActionTest7extendsAppBaseAction{}// Creates action with no payload, no meta, type 'app:ActionTest7' and error = false

UsingsetPrefix

import{ActionStandard,setPrefix}from'flux-action-class'// Add it only once and it changes default prefix for all actionssetPrefix('app:')classActionTest7extendsActionStandard{}// Creates action with no payload, no meta, type 'app:ActionTest7' and error = false

Usage in reducers

Be aware, if you're usingswitch-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 fieldtype.

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 usingngrx-actions instead.

You can take a look at the discussionhere

Usage in production. Minification.

flux-action-class relies on class names andTerser (Uglify) by default doesn't preserve class names during minification. Here's what you can do to change that.

Addkeep_classnames: true (keep_fnames: true for ES5 targets) toterserOptions.

module.exports={optimization:{minimizer:[newTerserPlugin({terserOptions:{// add this linekeep_classnames:true,},}),],},}

CRA doesn't expose Terser config by default (please, upvotethis issue to change it!). Rour options are:

After that you can follow instructions for tuning terser-webpack-plugin listed above.

Angular

Unfortunately, Angular team doesn't allow us to tune Terser with their default builder (please, upvotethis issue to change it!). You could use thiscustom builder, which is just a subclass of their default builder with Terser-tuning added.

About

Boilerplate free class-based action creator. Following flux-standard-action spec. Built with TypeScript. Works with redux and ngrx.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp