【发布时间】:2017-07-23 03:53:21
【问题描述】:
我正在阅读下面的示例中使用 React、Redux 和 Thunk 编写的 JavaScript:
在actions.js中创建动作:
function fetchPosts(subreddit){
return dispatch => {
dispatch(requestPosts(subreddit))
return fetch(`...URL...`)
.then(response => response.json())
.then(json => dispatch(receivePosts(subreddit, json)))
}
}
export function fetchPostsIfNeeded(subreddit) {
return (dispatch, getState) => {
if (shouldFetchPosts(getState(), subreddit)) {
return dispatch(fetchPosts(subreddit))
}
}
“fetchPosts(subreddit)”返回什么?。
我不明白“退货=>”是什么。
这个函数是在容器中导入并用于调度动作,所以我 认为“dispatch”是从容器中的“react-redux”导入的函数,如下所示:
import {fetchPostsIfNeeded} from "../actions"
import {connect} from "react-redux"
...
componentDidMount() {
const { dispatch, selectedSubreddit } = this.props
dispatch(fetchPostsIfNeeded(selectedSubreddit))
}
...
箭头前的“dispatch”是指函数“dispatch(requestPosts(subreddit))”吗?
dispatch 是 ES2015 缩写为“(dispatch)”的参数吗?
【问题讨论】:
标签: reactjs redux react-redux redux-thunk