0.0.1 • Published 6 years ago

redux-thunk-ext v0.0.1

Weekly downloads
6
License
MIT
Repository
github
Last release
6 years ago

Redux Thunk Extension

npm install --save redux-thunk-ext

Then, to enable redux-thunk-ext, use applyMiddleware():

// store.js
import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk-ext';
import reducer from './reducer';


const middlewares = [
  thunk,
]

const storeEnhancers = compose(
  applyMiddleware(...middleware),
)

export const store = createStore(reducer, storeEnhancers)

Example

for my poor English, I just give a code to compare the difference between redux-thunk and redux-thunk-ext.

use redux-thunk

export function getComments() {
  return async (dispatch: Dispatch<GetComments>): Promise<void> => {
    dispatch(createGetComments())
    let action: GetComments = await (async()=> {
      let url = `/comments`
      try {
        let result = await axios.get(url)
        let { data } = result
        let { comments } = data.data
        return createGetComments(comments, SUCCESS)
      } catch( error ) {
        return createGetComments(error)
      }
    })()
    dispatch(action)
  }
}

use redux-thunk-ext

export function getComments() {
  return async (dispatch: Dispatch<GetComments>): Promise<GetComments> => {
    dispatch(createGetComments())
    let url = `/comments`
    try {
      let result = await axios.get(url)
      let { data } = result
      let { comments } = data.data
      return createGetComments(comments, SUCCESS)
    } catch( error ) {
      return createGetComments(error)
    }
  }
}

License

MIT