这三个点到底是什么意思,为什么我需要它们?
export function leadReducer(state: Lead[]= [], action: Action {
switch(action.type){
case ADD_LEAD:
return [...state, action.payload];
case REMOVE_LEAD:
return state.filter(lead => lead.id !== action.payload.id )
}
}
从Typescript(也从ES7)开始,这三个点被称为散布运算符。
散布运算符返回数组的所有元素。就像您将每个元素分别编写一样:
let myArr = [1, 2, 3];
return [1, 2, 3];
//is the same as:
return [...myArr];
这主要是语法糖,因为它是这样编译的:
func(...args);
对此:
func.apply(null, args);
在您的情况下,将其编译为:
return [...state, action.payload];
//gets compiled to this:
return state.concat([action.payload]);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句