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

Redux action creator building utilities

License

NotificationsYou must be signed in to change notification settings

adrianObel/redux-create-actions

Repository files navigation

Build Statusnpmjs-standard-style

redux-create-actions is a library that helps construct FSA compliant action creators and massively decreasing the amount of boilerplate necessary to generate constants and action creators.

Getting started

install

$ npm install --save redux-create-actions

or

$ yarn add redux-create-actions

Usage

Suppose we need to generate action creators and constants to filter loaded todos and also fetch remote todos

import{module,createAction,createAsyncAction}from'redux-create-actions'const{  actions,  constants}=module('@todos',{filterTodos:createAction('FILTER_TODOS'),fetchTodos:createAsyncAction('FETCH_TODOS')})

Themodule block noted in the example above would return an object with a shape of

{constants:{'FILTER_TODOS':'@todos/FILTER_TODOS','FETCH_TODOS':'@todos/FETCH_TODOS','FETCH_TODOS_START':'@todos/FETCH_TODOS_START','FETCH_TODOS_SUCCESS':'@todos/FETCH_TODOS_SUCCESS','FETCH_TODOS_FAILURE':'@todos/FETCH_TODOS_FAILURE'},actions:{filterTodos:Fn,fetchTodos:Fn-fetchTodos.start:Fn-fetchTodos.success:Fn-fetchTodos.failure:Fn}}

Documentation

createAction

createAction(actionType:string)->ActionCreator

Utility function for generating action creators

ActionCreator=(payload,meta)->{ type, payload, meta}

createAsyncAction

createAsyncAction(actionType:string)->AsyncActionCreator

Utility function for generating async action creators. This will output a function (action creator) withstart,success, andfailure invokable properties for ease of use.

AsyncActionCreator=ActionCreatorAsyncActionCreator.start=ActionCreatorAsyncActionCreator.success=ActionCreatorAsyncActionCreator.failure=ActionCreator

module

module(namespace:string,actions:Object)->{ constants, actions}

namespace optional string to scope actions generated by module.actions object of action creators

{filterTodos:createAction('FILTER_TODOS'),fetchTodos:createAsyncAction('FETCH_TODOS')}

Motivation

Redux brought along many great things to the world of software development, unfortunately that came with a bit of extra boilerplate. While the boilerplate is manageable repeating the same code time and time again grows tiresome, especially when it comes to writing constants and action creators.

Take a usual file that exports constants and action creators for both sync and async actions.

constLOAD_POSTS='@home/LOAD_POSTS'constLOAD_POSTS_REQUEST={START:'@home/LOAD_POSTS_REQUEST_START',SUCCESS:'@home/LOAD_POSTS_REQUEST_SUCCESS',FAILURE:'@home/LOAD_POSTS_REQUEST_FAILURE'}exportconstconstants={LOAD_POSTS,LOAD_POSTS_REQUEST}constloadPosts=({ payload})=>({type:LOAD_POSTS, payload})constloadPostsRequest={start:({ payload})=>({type:LOAD_POSTS_REQUEST.START, payload}),success:({ payload})=>({type:LOAD_POSTS_REQUEST.SUCCESS, payload}),failure:({ payload})=>({type:LOAD_POSTS_REQUEST.FAILURE, payload})}exportconstactions{  loadPosts,  loadPostsRequest}

Writing action creators and constants like this quickly becomes very tedious work, and that's whereredux-create-actions comes in. The previous module would condense down to

import{module,createAsyncAction}from'redux-create-actions'const{  constants,  actions}=module('home',{loadPosts:createAsyncAction('LOAD_POSTS')})

License

MIT

About

Redux action creator building utilities

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp