Javascript react redux中间件调度2个操作
下面作为动作创建者的第一个区块与thunk一样工作,但我还想应用第二个区块,这是一个promise中间件。我如何调整它,使它可以调度2个动作Javascript react redux中间件调度2个操作,javascript,reactjs,redux,middleware,react-redux,Javascript,Reactjs,Redux,Middleware,React Redux,下面作为动作创建者的第一个区块与thunk一样工作,但我还想应用第二个区块,这是一个promise中间件。我如何调整它,使它可以调度2个动作 export const fetchPokemon = function (pokemonName) { return function (dispatch) { dispatch({type: 'REQUESTING'}) const requestURL = `http://pokeapi.co/api/v2/pokemon/${p
export const fetchPokemon = function (pokemonName) {
return function (dispatch) {
dispatch({type: 'REQUESTING'})
const requestURL = `http://pokeapi.co/api/v2/pokemon/${pokemonName}/`
return fetch(requestURL)
.then(function (response) {
return response.json()
})
.then(function (data) {
dispatch(receivePokemon(formatPokemonData(data)))
dispatch(fetchPokemonDescription(pokemonName))
})
}
}
中间件
const fetchPromiseMiddleware = store => next => action => {
if (typeof action.then !== 'function') {
return next(action)
}
return Promise.resolve(action).then(function (res) {
if (res.status >= 400) {
throw new Error("Bad response from server")
}
return res.json()
}).then(store.dispatch)
}
我尝试了以下操作,但出现错误:
store.js:33未捕获(承诺中)类型错误:(0,
_actionCreators.receivePokemon)不是一个函数
您的问题中没有足够的代码,但是当您在显示的代码中调用
receivePokemon(formatpokemonda(data))
时,receivePokemon
不是一个函数,现在您需要检查该函数的定义位置,它可能不是。?
const fetchPromiseMiddleware = store => next => action => {
if (typeof action.then !== 'function') {
return next(action)
}
return Promise.resolve(action).then(function (res) {
if (res.status >= 400) {
throw new Error("Bad response from server")
}
return res.json()
}).then(function (data) {
return store.dispatch(receivePokemon(formatPokemonData(data)))
}).then(function (data) {
return store.dispatch(fetchPokemonDescription(data.name))
})
}