我有以下Axios的请求:在何处添加服务器请求功能阵营-Redux的
componentDidMount() {
axios.post('http://localhost:3030/api/weather/refresh').then(response => {
store.dispatch(refreshWeather(response))
});
}
此发送派遣终极版,其使用在通用模式喂表象容器。
我的问题是 - 我该如何让它做到这一点axios.post()请求可重用在应用程序的其他组件或部分?我已经尽可能在导入的外部文件中使用它,但有没有更好的方法来构建我的项目,以便将所有的axios请求分组在一起?
我有以下mapDispatchToProps:
const mapDispatchToProps = (dispatch, ownProps) => {
return {
onClickRefresh:() => {
dispatch(refreshWeather(arr))
}
}
}
我想运行的componentDidMount()相同的请求,但不知道的最好的方法,使重复使用如上所述。
感谢
见http://stackoverflow.com/questions/35411423/how-to-dispatch-a-redux -action-with-a-timeout/35415559#35415559,http://stackoverflow.com/questions/34570758/why-do-we-need-middleware-for-async-flow-in-redux/34599594#34599594,以及http://blog.isquaredsoftware.com/2016/10/idiomatic-redux-why-use-action-creators/了解如何将异步逻辑移入行动创建者(如thunk)的信息。 – markerikson