reactjs - 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 运行良好,但调度不起作用。
请帮我
解决方案
似乎解决方案很简单:您忘记在调用操作的代码段中
发送dispatch
withgetCity
// Change this
getCity(data)
// To this
getCity(data, dispatch)
推荐阅读
- php - 通过 url 将产品添加到购物车会在第一次访问时提供 404 并在第 2 次访问
- sql-server - SQL Server 查询以进行借方/贷方指定表中的计算列
- azure-cosmosdb - 检查数组的内容——CosmosDB
- c# - Url.Action(params) 在末尾添加 ')'
- perf - perf 报告单个睡眠的多个 sched:sched_stat_sleep 事件
- javascript - 在 React 中传递函数和钩子?还是把它们放在一起?
- javascript - 如何将txt文件中的数据保存到函数外的数组中?
- r - 为循环中的多个绘图绘制 x 轴
- dataset - Power BI - 保留策略 - basicFIFO
- c# - 如何在 EasyNetQ 中获取现有的 Exchange 或队列?