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 {
...
最佳答案
连续的两个箭头函数称为 thunk。
简单来说,这种格式更容易理解:
function updateSearchTopStoriesState = (hits, page) {
function updateState (prevState) {
...code
}
return updateState;
}
关于javascript - ReactJS中的提升函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58897534/