0.0.1 • Published 7 years ago
redux-thunk-ext v0.0.1
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
0.0.1
7 years ago