首页 > 解决方案 > redux:调度不起作用

问题描述

我正在使用 redux 开发一个反应应用程序我在一个单独的 js 文件中编写了我的操作,如下所示

function getCity(city,dispatch) {
  fetch('https://################', {
    method: "GET",
    headers: {
      "user-key": "#############",
      "Content-Type": "application/json",
    },
  }).then((res) => {
    return res.json()
  }).then((data) => {
    console.log(data.location_suggestions);
    dispatch({type:'getting_cities', city:data.location_suggestions});
  })
}

然后我用下面的代码映射它们

function mapDispatchToProps(dispatch) {
  return {
    getLocations:(data) => getCity(data),
    logStore: () => dispatch({type:'LOGSTORE'})
  }
}

console.log 运行良好,但调度不起作用。

请帮我

标签: reactjsreduxreact-redux

解决方案


似乎解决方案很简单:您忘记在调用操作的代码段中
发送dispatchwithgetCity

// Change this
getCity(data)

// To this
getCity(data, dispatch)

推荐阅读