Skip to content

YikaJ/redux-promise-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

redux-promise-utils

Based on redux-thunk & redux-actions.

Easy utils to handle Promise in redux.

Quick Start

Install

use npm

npm i redux-promise-utils --save

use yarn

yarn add redux-promise-utils

Usage

  1. Ensure your reducer is handled by redux-thunk.
  2. Ensure your api is a Promise.
// data/action.js
import { createPromiseAction } from 'redux-promise-utils'
import * as api from './api'

export const fetchAction = createPromiseAction('fetchList', api.fetchList)

// data/reducer.js
import { createReducer } from 'redux-promise-utils'
import { fetchAction } from './action'

const initState = {}

const reducer = createReducer(initState)
    // catch sync action
    .which('SYNC_ACTION', (state, action) => {
        return state
    })
    // catch async action with three state
    .asyncWhich(fetchAction, {
        start(state, action) {
            return state
        },
        success(state, action) {
            return state
        },
        fail(state, action) {
            return state
        }
    })
    // build redux reducer handler
    .build()

export default reducer

Releases

No releases published

Packages

No packages published