All Projects → AndersDJohnson → redux-reducer-async

AndersDJohnson / redux-reducer-async

Licence: MIT license
Create redux reducers for async behaviors of multiple actions.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to redux-reducer-async

hermes-js
Universal action dispatcher for JavaScript apps
Stars: ✭ 15 (+7.14%)
Mutual labels:  flux, promise, reducer, action
k-redux-factory
Factory of Redux reducers and their associated actions and selectors.
Stars: ✭ 18 (+28.57%)
Mutual labels:  actions, reducer, action, reducers
Deox
Functional Type-safe Flux Standard Utilities
Stars: ✭ 200 (+1328.57%)
Mutual labels:  flux, reducer, action
riduce
Get rid of your reducer boilerplate! Zero hassle state management that's typed, flexible and scalable.
Stars: ✭ 14 (+0%)
Mutual labels:  reducer, reducers, reducer-creation
RxReduxK
Micro-framework for Redux implemented in Kotlin
Stars: ✭ 65 (+364.29%)
Mutual labels:  flux, reducer, action
fluxy
Fluxy is a Flux architecture implementation written in Kotlin.
Stars: ✭ 25 (+78.57%)
Mutual labels:  flux, flux-architecture
nanoflux-fusion
Redux-like extension for Nanoflux
Stars: ✭ 15 (+7.14%)
Mutual labels:  flux, flux-architecture
promiviz
Visualize JavaScript Promises on the browser. Visualize the JavaScript Promise APIs and learn. It is a playground to learn about promises faster, ever!
Stars: ✭ 79 (+464.29%)
Mutual labels:  promises, promise
market-pricing
Wrapper for the unofficial Steam Market Pricing API
Stars: ✭ 21 (+50%)
Mutual labels:  promises, promise
bluff
🙏 Promise A+ implementation
Stars: ✭ 14 (+0%)
Mutual labels:  promises, promise
alls
Just another library with the sole purpose of waiting till all promises to complete. Nothing more, Nothing less.
Stars: ✭ 13 (-7.14%)
Mutual labels:  promises, promise
setup-lazarus
Set up your GitHub Actions workflow with a specific version of Lazarus
Stars: ✭ 29 (+107.14%)
Mutual labels:  actions, action
pr-compliance-action
Check PR for compliance on title, linked issues, and files changed
Stars: ✭ 151 (+978.57%)
Mutual labels:  actions, action
gh-actions-html-table-generator
Read from a json file and write to the README
Stars: ✭ 29 (+107.14%)
Mutual labels:  actions, action
Smitty
Tiny flux implementation built on mitt
Stars: ✭ 210 (+1400%)
Mutual labels:  flux, flux-architecture
release-changelog-builder-action
A GitHub action that builds your release notes / changelog fast, easy and exactly the way you want.
Stars: ✭ 515 (+3578.57%)
Mutual labels:  actions, action
redux-entities
Higher-order reducer for store entities received from normalizr and makes it easy to handle them.
Stars: ✭ 34 (+142.86%)
Mutual labels:  reducer, reducers
dont-waste-your-ducking-time
🐓 An opinionated guide on how to test Redux ducks
Stars: ✭ 28 (+100%)
Mutual labels:  actions, reducers
action-dynamic-readme
~ Dynamic ReadME Generator ~
Stars: ✭ 29 (+107.14%)
Mutual labels:  actions, action
Fetch
Asynchronous HTTP client with promises.
Stars: ✭ 29 (+107.14%)
Mutual labels:  promises, promise

redux-reducer-async

Create redux reducers for async behaviors of multiple actions.

npm Travis CI Codecov

Be DRY & reduce boilerplate. Standardize state schema with managed properties for loading, success, and error cases.

Think of it as redux-actions for asynchronous reducers.

Works with Flux Standard Actions (FSA). By default, supports the action type conventions of redux-promise-middleware, but see Custom Action Types below for configuration to support redux-promise.

Install

npm install --save redux-reducer-async (copy)

Use

import createReducer from 'redux-reducer-async'

const myActionReducer = createReducer('MY_ACTION')

results in a reducer like this:

(state = {}, action = {}) => {
  switch (action.type) {
    case 'MY_ACTION_PENDING':
      return { ...state, loading: true, error: null }
    case 'MY_ACTION_FULFILLED':
      return { ...state, loading: false, error: null, data: action.payload }
    case 'MY_ACTION_REJECTED':
      return { ...state, loading: false, error: action.payload }
    default:
      return state
  }
}

You can then mount it with combineReducers:

import { combineReducers } from 'redux'
import createReducer from 'redux-reducer-async'

const rootReducer = combineReducers({
  myAction: createReducer('MY_ACTION')
})

Or even call it manually within another reducer (useful with custom properties or reducers):

import createReducer from 'redux-reducer-async'

const myActionReducer = createReducer('MY_ACTION')

const reducer = (state = {}, action = {}) => {
  state = myActionReducer(state, action)
  // ...
  return state
}

Custom Properties

You can provide custom property names (all optional) for each case to be used on the state:

createReducer('MY_ACTION', {
  loading: 'isMyActionLoading',
  success: 'myActionData',
  error: 'myActionError'
})

Custom Reducers

You can also provide custom reducer functions (again all optional, but be careful to define all cases if you use non-standard property names in one):

createReducer('MY_ACTION', {
  loading: state => ({
    ...state,
    myActionError: null,
    myActionIsLoading: true,
    extra: 'whatever'
  })
  // success, error...
})

And you can even mix these with custom properties:

createReducer('MY_ACTION', {
  loading: 'isLoading',
  error: (state, action) => ({
    ...state,
    isLoading: false,
    error: action.payload,
    also: 'etc'
  })
})

Custom Action Types

You can provide custom action types.

For example, to support redux-promise, which uses same the action type for success and error cases (though it does not provide a loading action), you can use finalActionType:

import createReducer, { finalActionType } from 'redux-reducer-async'

createReducer(finalActionType('MY_ACTION'))

which is effectively like providing custom action types:

createReducer({
  loading: 'MY_ACTION_PENDING',
  success: 'MY_ACTION',
  error: 'MY_ACTION'
})

Or similarly by passing suffixes to the actionTypes helper, which is normally used to explicitly define all types:

import createReducer, { actionTypes } from 'redux-reducer-async'

createReducer(actionTypes('MY_ACTION', '_LOADING', '_SUCCESS', '_ERROR'))

But can also be used to suppress suffixes (here undefined means use default):

createReducer(actionTypes('MY_ACTION', undefined, '', ''))

Transforms

As a shortcut to defining custom reducers, you can provide transform functions to manipulate only the payload, optionally in success and/or error cases:

createReducer('MY_ACTION', {
  transform: payload => ({
    ...payload,
    title: payload.title.trim()
  }),
  transformError: payload => ({
    ...payload,
    message: `There was an error: ${payload.message}`
  })
})
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].