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

Lightweight, no dependency library for redux reducers

License

NotificationsYou must be signed in to change notification settings

d1slike/redux-utils

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

npm versionBuild StatusCoverage Status

Lightweight, no dependency library for redux reducers

Install

npm install --save redux-reducer-utils

Or

yarn add redux-reducer-utils

Usage

Reducer creation

createReducer provides more declarative way to describe redux reducer

Just callcreateReducer with initial state as argument and theninvoke chainwhen to register actions handlers, finally calltoFunction to build reducer

import{createReducer}from'redux-reducer-utils';constinitialState={a:1,b:2,c:3,d:4,};constreducer=createReducer(initialState).when(ACTION_TYPE_1,(state,action)=>({        ...state,a:state.a+action.payload,})).when(ACTION_TYPE_4,(state,action)=>({        ...state,c:state.c+action.payload,})).toFunction();

Here comparison classic reducer description withcreateReducer way

constACTION_TYPE_1='ACTION_TYPE_1';constACTION_TYPE_2='ACTION_TYPE_2';constACTION_TYPE_3='ACTION_TYPE_3';constACTION_TYPE_4='ACTION_TYPE_4';constinitialState={a:1,b:2,c:3,d:4,};constclassicReducer=(state=initialState,action)=>{switch(action.type){caseACTION_TYPE_1:return{                ...state,a:state.a+action.payload,};caseACTION_TYPE_2:caseACTION_TYPE_3:return{                ...state,b:state.b+action.payload,};caseACTION_TYPE_4:return{                ...state,c:state.c+action.payload,};default:returnstate;}};constwithCreateReducer=createReducer(initialState).when(ACTION_TYPE_1,(state,action)=>({        ...state,a:state.a+action.payload,}))//if you need the same handler for two or more actions types, just put array to first argument.when([ACTION_TYPE_2,ACTION_TYPE_3],(state,action)=>({        ...state,b:state.b+action.payload,})).when(ACTION_TYPE_4,(state,action)=>({        ...state,c:state.c+action.payload,})).toFunction();

Reducer composition

composeReducers This function can help with reducers horizontal scaling

Everyi reducer has access toi-1 state

import{composeReducers}from'redux-reducer-utils';constSOME_ACTION='SOME_ACTION';constinitialStateFirst={a:1,b:2,};constinitialStateSecond={c:3,d:3,};constfirstReducer=(state=initialStateFirst,action)=>{//...some actions handlers};constsecondReducer=(state=initialStateSecond,action)=>{//...here you have access to state from firstReducerswitch(action.type){caseSOME_ACTION:return{                ...state,a:state.a+action.payload,};default:returnstate;}};exportconstcomposedReducer=composeReducers(firstReducer,secondReducer,/*...any number of reducers*/);

About

Lightweight, no dependency library for redux reducers

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp