【发布时间】:2018-08-16 16:55:08
【问题描述】:
我正在尝试修改减速器中“持续时间”属性的值。 当我控制台记录个人“合作”时,一切都很好。但是,我无法正确更新我的 newState。
你有什么想法吗?
export function reducer(state = initialState, action) {
let newState = [...state];
switch (action.type) {
case "SEARCH_PEOPLE":
newState = initialState;
let results;
axios
.get(`https://maps.googleapis.com/maps/api/distancematrix/json?origins=${origins}&destinations=${destination}®ion=FR&key=${config.gmap.key}`)
.then(res => {
results = res.data.rows;
newState = newState.map((collab, i) => {
return console.log("collab : ", {
...collab,
duration: results[i].elements[0].duration.text
})
}
)
})
return newState;
default:
break;
}
return newState;
}
提前致谢!
【问题讨论】:
-
这是不正确的方式。您还需要使用 redux 中间件,例如
redux-thunk、redux-saga或您想要的任何其他中间件。这是因为您调用的 API 需要async方法,而 redux 在不使用中间件时是同步的。如果您想使用redux-saga并了解如何实现github.com/sghaleb1/redux-saga-boilerplate,我有样板文件 -
谢谢!我会看看这个!
-
我建议查看“redux-axios-middleware”github.com/svrcekmichal/redux-axios-middleware。基本上,它会自动为您添加一个
SEARCH_PEOPLE_SUCCESS操作。