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