【发布时间】:2016-08-30 09:18:09
【问题描述】:
我想将一个布尔值作为第二个参数传递给我的actionCreator,这将决定我的中间件调度什么,但是我如何让我的中间件访问这个第二个参数?
我是否必须调度一个数组或对象而不是一个承诺?
export const fetchPokemon = function (pokemonName, booleanValue) {
return function (dispatch) {
dispatch({type: 'REQUESTING'})
const requestURL = `http://pokeapi.co/api/v2/pokemon/${pokemonName}/`
dispatch(fetch(requestURL))
}
}
中间件
const fetchPromiseMiddleware = store => next => action => {
if (typeof action.then !== 'function') {
return next(action)
}
...
return response.json()
}).then(function (data) {
if booleanValue {
store.dispatch(receivePokemon(formatPokemonData(data)))
} else {
store.dispatch(fetchPokemonDescription(data.name))
}
})
}
【问题讨论】:
标签: javascript reactjs redux middleware react-redux