在const {defaults} = require('lodash/fp')
const withDefaultStatus = defaults({
isFinished: false,
isPending: true,
code: 0
});
const state = {
isFinished: true,
isMutation: false,
isPending: false,
lastUpdated: 1512730452993,
queryCount: 5,
status: 1,
url: "https://",
}
function selectQueryStatus(state) {
const {
isFinished,
isPending,
status: code,
lastUpdated,
queryCount
} = state || {};
return withDefaultStatus({
isFinished, isPending , ...code && { code, lastUpdated, queryCount }
});
}
console.log(selectQueryStatus(state))
//output { isFinished: true, isPending: false, code: 1, lastUpdated: 512730452993, queryCount: 5 }
中使用展开运算符作为参数有什么意义?
我一直在盯着这个代码看一段时间,我无法弄明白 起初我以为它被用作条件,但现在我完全糊涂了。
PS1:我认为它与lodash没有任何关系,但无论如何我都加了它 PS2:状态是从React组件传递的。tofixed()
答案 0 :(得分:2)
使用以下语法
withDefaultStatus({
isFinished, isPending , ...code && { code, lastUpdated, queryCount }
});
使用Spread语法的原因是,只有在代码可用时才需要将code
,lastUpdate
和queryCount
值传递给对象。因此,当…code && { code, lastUpdated, queryCount }
被评估为code, lastUpdated, queryCount
时,code
基本上会返回true
。因此,整个对象将成为
{
isFinished,
isPending ,
code,
lastUpdated,
queryCount
}