【发布时间】:2017-04-24 17:25:10
【问题描述】:
我编写了以下 thunk action creator,用于向 api 发出请求。
如果我的理解是正确的,thunk action creator 将由中间件处理并可以访问 store 调度方法。
让这个 thunk 动作创建器可用于 React 组件的惯用方式是什么?
我能想到的最好的方法就是直接导入thunk action creator。
export function fetchMovie(title) {
return (dispatch) => {
dispatch(requestMovie(title));
const url = `http://www.omdbapi.com/?t=${title}&y=&plot=short&r=json`
return axios.get(url)
.then(response => {
dispatch(receiveMovie(title, response.data))
})
.catch(err => dispatch(requestMovieErr(title, err)))
}
}
【问题讨论】: