[英]react redux middleware dispatch 2 actions
The first chunk as an action creator below works as is with thunk, but I want to also apply the 2nd chunk, which is a promise middleware. 下面作为动作创建者的第一个块与thunk一样工作,但是我还想应用第二个块,这是一个承诺中间件。 How do I tweak it so that it can dispatch 2 actions?
我如何对其进行调整,以便它可以调度2个动作?
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))
})
}
}
middleware 中间件
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)
}
I've tried the below but get an error: 我已经尝试了以下方法,但出现错误:
store.js:33 Uncaught (in promise) TypeError: (0 , _actionCreators.receivePokemon) is not a function
store.js:33未捕获(承诺)TypeError:(0,_actionCreators.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))
})
}
您的问题中没有足够的代码,但是似乎当您在显示的代码中调用receivePokemon(formatPokemonData(data))
时, receivePokemon
不是函数,现在您需要检查定义的位置,可能不是。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.