Skip to content

Async action creator for use with redux and redux-thunk.

Notifications You must be signed in to change notification settings

doordash/async-actions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

async-actions

Async action creator for use with redux and redux-thunk. Uses whatwg-fetch and dispatches IN_PROGRESS, SUCCESS, and FAIL events during the lifecycle of an async request.

actions.js

import { createAction, makeAsyncActionTypes } from 'async-actions'

export const {
  LIST_USERS,
  CREATE_USER,
} = makeAsyncActionTypes('DoorDash', [
  'LIST_USERS',
  'CREATE_USER',
])

export const listUsers = createAction(LIST_USERS, '/api/users/')
export const createUser = email => createAction(CREATE_USER, '/api/users/', {
  method: 'POST',
  body: {
    email,
  },
})

reducers.js

import { combineReducers } from 'redux'
import {
  LIST_USERS,
  CREATE_USER,
} from './actions'

// highly suggested: using a library like https://github.com/gaearon/normalizr to flatten state
function users(state = [], action) {
  if (action.type === LIST_USERS.SUCCESS) {
    return [...action.response]
  } else if (action.type === CREATE_USER.SUCCESS) {
    return [...state, action.response]
  }

  return state
}

export default combineReducers({
  users,
})

About

Async action creator for use with redux and redux-thunk.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published