0
const updateSearchTopStoriesState = (hits, page) => (prevState) => {
const { searchKey, results } = prevState

任何人都可以向我解释上述内容是做什么的吗?为什么有 2 个箭头功能?

这是整个代码

const updateSearchTopStoriesState = (hits, page) => (prevState) => {
const { searchKey, results } = prevState;
const oldHits = results && results[searchKey]
? results[searchKey].hits
: [];
const updatedHits = [
...oldHits,
...hits
];
return {
results: {
...results,
[searchKey]: { hits: updatedHits, page }
},
isLoading: false
};
};
class App extends Component {
...
4

2 回答 2

1

连续的两个箭头函数称为 thunk。

简单地说,以这种格式更容易理解:

function updateSearchTopStoriesState = (hits, page) {
   function updateState (prevState) {
    ...code
   }
   return updateState;
}
于 2019-11-17T05:15:16.303 回答
0

箭头函数中的箭头表示隐式返回。因此,如果您看到两个箭头相互跟随,则表示第一个函数返回一个函数。这不是 React 的东西,而是 ES2016 的东西。如果是这样,上面的答案将是正确的:

function updateSearchTopStoriesState = (hits, page) {
   return function updateState(prevState) {
    ...code
   }
//Anything you put here would be disregarded since it is after the return statement.
}

将此视为一个基本示例:

const add = num1 => num2 => num3 => num1+num2+num3;

console.log(add(3)(4)(5));
于 2019-11-17T06:28:15.223 回答