我有以下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