redux-thunk-ext
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

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

Dependencies (0)

    Dev Dependencies (35)

    Package Sidebar

    Install

    npm i redux-thunk-ext

    Weekly Downloads

    1

    Version

    0.0.1

    License

    MIT

    Last publish

    Collaborators

    • lemonclown
    • lemon-clown-owner