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

Type friendly reducer helper

NotificationsYou must be signed in to change notification settings

mizchi/hard-reducer

Repository files navigation

CircleCIGreenkeeper badge

Type friendly facade for better reducer.

npm install hard-reducer --save# oryarn add hard-reducer

Concepts

  • Type safe interface
  • Avoid redundanttype string definitions
  • Keep reducer interface(State, Action) => State to use withredux.combineReducers()
  • Handle Flux Standard Action<Payload>{ type: string, payload: Payload }

Check this code to know detail.

Flow playground

Examples

This code is runnable in both flowtype and typescript

/*@flow */// ^ flow magic comment to activate. It will be ignored in typescript.import{buildActionCreator,createReducer,typeActionCreator}from"hard-reducer";// ^ If you use typescript, Do not use `type` before ActionCreatorconst{ createAction}=buildActionCreator({prefix:"counter/"});// Add type to your payload by ActionCreatorconstinc:ActionCreator<number> = createAction("inc");// or infer by function resultconst dec = createAction("dec", (val: number) =>val);inc(1);//=> { type: 'counter/inc', payload: 1 }// Define state typetypeState={value:number};constinitialState:State={value:0};constreducer=createReducer(initialState)// Handle `(State, Payload) => State` in matched context..case(inc,(state,payload)=>{return{value:state.value+payload};}).case(dec,(state,payload)=>{// $ExpectErrorconstp:string=payload;return{value:state.value-payload};})// Take string.case("other/noop",(state,payload)=>{returnstate;})// Take all uncaught action, not payload!.else((state,action)=>{console.log("default fallback");returnstate;});// Use itconstret0=reducer(initialState,inc(3));constret1=reducer(ret1,dec(1));

See detail inindex.js.flow orindex.d.ts

Handle async action: createAsyncAction

createAsyncAction(...) returns{ resolved, rejected, started } and callable method.

(You need to addredux-thunk in store's middlewares)

/*@flow */import{createReducer,buildActionCreator}from"hard-reducer";const{ createAsyncAction}=buildActionCreator();constincAsync=createAsyncAction("inc-async",async(val:number)=>{if(val%2===1){thrownewError("error");}return{p:1};});typeStatus="ready"|"started"|"resolved"|"rejected";typeState={status:Status,payload: ?{p:number}};constreducer=createReducer({status:"ready",payload:null}).case(incAsync.started,state=>{return{state:"started"};}).case(incAsync.resolved,(state,payload)=>{return{state:"resolve", payload};}).case(incAsync.rejected,(state,error)=>{return{state:"ready",payload:null};});// storeimportreduxThunkfrom"redux-thunk";import{applyMiddleware,createStore}from"redux";conststore=createStore(reducer,undefined,applyMiddleware(reduxThunk));store.subscribe((...args)=>{console.log("store",store.getState());});// dispatchstore.dispatch(incAsync(1));

Handle thunk action: createThunkAction

createThunkAction(...) returns{ resolved, rejected, started } and callable method.

(You need to addredux-thunk in store's middlewares)

import{createReducer,buildActionCreator}from"hard-reducer";const{ createThunkAction, createAction}=buildActionCreator();constinc=createAction("inc",(val:number)=>val);constthunked=createThunkAction("thunked",async(input,dispatch,getState)=>{dispatch(inc(input.value));return{ret:true};});// HandlecreateReducer({status:"ready",payload:null}).case(thunked.started,state=>{return{state:"started",payload:null};}).case(thunked.resolved,(state,payload)=>{return{state:"resolve", payload};}).case(thunked.rejected,(state,error)=>{return{state:"ready",payload:null};});// dispatchstore.dispatch(thunked({value:1}));

Related projects

ChangeLog

SeeChangeLog.md

LICENSE

MIT

About

Type friendly reducer helper

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors5


[8]ページ先頭

©2009-2025 Movatter.jp