Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

redux-action-tools

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

redux-action-tools

redux action tools with full async support inspired by redux-action

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

redux-action-tools

This lib is inspired by redux-actions and the redux-promise-thunk

中文版README

Install

npm i redux-action-tools

Usage

createAction(actionName, payloadCreator [, metaCreator])

The same as createAction in redux-actions;

createAsyncAction(actionName, promiseCreator [, metaCreator])

You will need to use redux-thunk as middleware to make this function work.

The createAction will return an action creator which creates pain action object, while this function will return a action creator for thunk

And instead of payloadCreator, you need to pass a promiseCreator which returns a promise object.

See examples below:

//editTodo is a thunk
const editTodo = createPromiseThunk('EDIT_TODO', function(todo) {
  return todoApi.edit(todo); //todoApi.edit() should return a Promise object;
});

//TodoItem.jsx
const {editTodo} = bindActionCreators(actions, dispatch);

class TodoItem extends Component {
  //...
  handleEdit(todo) {
    editTodo(todo);//only one parameter is allowed, and will be passed to promiseCreator;
  }
  //...
}

The thunk function will dispatch following flux standard actions(FSA) for the promise you returned in promiseCreator:

typeWhenpayloadmeta.asyncPhase
${actionName}before promiseCreator been calledfirst argument of promiseCreator'START'
${actionName}_COMPLETEDpromise resolvedvalue of promise'COMPLETED'
${actionName}_FAILEDpromise rejectedreason of promise'FAILED'

The core idea here is the different action for completed or failed should have there own type, this make things more clear and closer to what we do in Elm.

With first action you can do optimistic update easily, and meta.asyncPhase enables you to intercept sync action or api failure in middleware, the example is WIP

createReducer

Write 'XX_COMPLETED', 'XX_FAILED' in reducer is awful? Here comes createReducer!

This function is super easy just check example:


const handler = (state, action) => newState

const reducer = createReducer()
  .when([ACTION_FOO, ACTION_BAR], handlerForBothActions)
  .when('BAZ', handler) // you can do optimistic update here, and it's ok to omit the handler if you want
  .done(handler) //handle 'BAZ_COMPLETED'
  .failed(errorHandler) //handle 'BAZ_FAILED'
  .build(initValue); // DON'T FORGET !

The done and failed method can only follow with when(action), so examples below are INVALID and will throw an error:


reducer = createReducer()
  .done(handler) // throw error here, cuz we don't know handle the success of which async action
  .build()

reducer = createReducer()
  .when([A, B])
  .done(handler) // throw error here, same reason since we don't know which one you want

Keywords

FAQs

Package last updated on 01 Sep 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc