[英]Chained Arrow function syntax
const fetch = url => dispatch => {
// ...
}
export const fetchQuestions = tag => (dispatch) => {
return dispatch(fetch(tag));
};
什么是fetch
函数中的dispatch
? url
是第一个和单个参数fetch
功能。 但是什么在这里dispatch
?
这相当于一个函数返回另一个函数。 就是这个
const fetch = url => dispatch => {
// ...
}
相当于
const fetch = function(url) {
return function(dispatch) {
// ...
}
}
同样这个
export const fetchQuestions = tag => (dispatch) => {
return dispatch(fetch(tag));
};
相当于
export const fetchQuestions = function(tag) {
return function(dispatch) {
return dispatch(fetch(tag));
}
};
dispatch
是url => ...
函数返回的函数的第一个和单个参数。 使用正常的函数语法,它将是
const fetch = function(url) {
return function(dispatch) {...}
}
它是编写returns another function
的函数的较短方式。 参数url
和dispatch
是curryed函数的参数。箭头函数语法的ES5等价物
function fetch(url) {
return function(dispatch) {
....
}
}
或者使用箭头语法
const fetch = (url) => {
return (dispatch) => {
// ...
}
}
同样,你可以将fetchQuestion
写为
export function fetchQuestions(tag) {
return function(dispatch){
return dispatch(fetch(tag));
}
}
或者使用箭头语法
export const fetchQuestions = (tag) => {
return (dispatch) => {
return dispatch(fetch(tag));
}
};
fetch
是一个命名函数表达式,它接受url
参数并返回一个带有dispatch
参数的新函数。
您可以使用传统的函数语法重写:
const fetch = function (url) {
return function(dispatch) {
// ...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.