Reactjs 归还什么?
我正在阅读以下示例中使用React、Redux和Thunk编写的JavaScript: 在actions.js中创建操作:Reactjs 归还什么?,reactjs,redux,react-redux,redux-thunk,Reactjs,Redux,React Redux,Redux Thunk,我正在阅读以下示例中使用React、Redux和Thunk编写的JavaScript: 在actions.js中创建操作: function fetchPosts(subreddit){ return dispatch => { dispatch(requestPosts(subreddit)) return fetch(`...URL...`) .then(response => res
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是否是一个参数,其中“(dispatch)”缩写为ES2015?来自:
因此,dispatch=>{…}
正在生成一个带有一个参数的箭头函数,dispatch
fetchPosts(subreddit)
返回单个箭头函数
关于{disppatch,selectedSubreddit}=this.props
的废话就是所谓的解构赋值。手册:
其实质是:
var o = {p: 42, q: true};
var {p, q} = o;
console.log(p); // 42
console.log(q); // true
所以是的,dispatch
是从这个.props中提取出来的,这显然是一个组件 谢谢大家!!我理解这个“分派”是一个函数,返回一个箭头函数。当此函数中的所有“dispatch”都更改为其他名称时,此函数仍按您所说的那样工作。非常感谢你!
var o = {p: 42, q: true};
var {p, q} = o;
console.log(p); // 42
console.log(q); // true