首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >响应redux中间件分派2项操作

响应redux中间件分派2项操作
EN

Stack Overflow用户
提问于 2016-08-30 06:52:30
回答 1查看 326关注 0票数 2

下面作为动作创建者的第一个块与thunk一样工作,但我也想应用第二个块,这是一个承诺中间件。我如何调整它,以便它可以分派两个动作?

代码语言:javascript
复制
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))
    })
  }
}

中间件

代码语言:javascript
复制
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 Uncaught (承诺) TypeError:(0,_actionCreators.receivePokemon)不是函数

代码语言:javascript
复制
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))
  })
}
EN

回答 1

Stack Overflow用户

发布于 2016-09-16 05:57:10

您的问题中没有足够的代码,但是当您在显示的代码中调用receivePokemon(formatPokemonData(data))时,receivePokemon不是一个函数,现在您需要检查定义在哪里,它可能不是。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39220645

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档