const fetch = url => dispatch => {
// ...
}
export const fetchQuestions = tag => (dispatch) => {
return dispatch(fetch(tag));
};
什么是dispatch
在fetch
函数中? url
是第一个和单个参数fetch
函数。但是dispatch
在这里?链式箭头功能语法
const fetch = url => dispatch => {
// ...
}
export const fetchQuestions = tag => (dispatch) => {
return dispatch(fetch(tag));
};
什么是dispatch
在fetch
函数中? 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 function的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) {
// ...
}
}
这就是所谓的钻营。去谷歌上查询。 –
你可以看到它的行动。有一些es6 => es5转换器在线。试试这个例子:https://es6console.com/。将代码粘贴到编辑器中,然后单击“变换” – Victor
将根据您的代表降低此值,但我意识到您没有参与JavaScript。 – BotNet