Type friendly facade for better reducer.
npm install hard-reducer -S
# or
yarn add hard-reducer
- Type safe interface
- Avoid redundant
type
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 */
import { buildActionCreator, createReducer } from 'hard-reducer'
const { createAction } = buildActionCreator({ prefix: 'counter/' })
const inc = createAction('inc', (val: number) => val)
const dec = createAction('dec', (val: number) => val)
inc(1) //=> { type: 'counter/inc', payload: 1 }
type State = { value: number }
const initialState = { value: 0 }
const reducer = createReducer(initialState)
// Handle `(State, Payload) => State` in matched context.
.case(inc, (state, payload) => {
return {
value: state.value + payload
}
})
.case(dec, (state, payload) => {
// $ExpectError
const p: string = payload
return {
value: state.value - payload
}
})
// Use it
const ret0 = reducer(initialState, inc(3))
const ret1 = reducer(ret1, dec(1))
Use with explicit types.
/* @flow */
import { buildActionCreator } from 'hard-reducer'
import type { ActionCreator, Reducer } from 'hard-reducer/types'
const { createAction } = buildActionCreator({ prefix: 'xxx/' })
const foo: ActionCreator<number, string> = createAction('foo', (val: number) =>
val.toString()
)
const reducer: Reducer<{
/* type definition */
}> = createReducer(/* initialState */)
See more on index.js.flow
- reduxactions/redux-actions: Flux Standard Action utilities for Redux.
- aikoven/typescript-fsa: Type-safe action creator utilities
- acdlite/flux-standard-action: A human-friendly standard for Flux action objects.
- TypeScript support by index.d.ts
- Error Cases
MIT