ESLint不喜欢在
这是不鼓励的,因为它会导致变量的作用域超出当前的case
.通过使用块,可以将变量的范围限制到该块.
使用{}
创建带有大小写的块范围,如下所示:
case DELETE_INTEREST: {
let .....
return (...)
}
查看以下片段:
function withOutBraces() {
switch(1){
case 1:
let a=10;
console.log('case 1', a);
case 2:
console.log('case 2', a)
}
}
function withBraces() {
switch(1){
case 1: {
let a=10;
console.log('case 1', a);
}
case 2: {
console.log('case 2', a)
}
}
}
console.log('========First Case ============')
withOutBraces()
console.log('========Second Case ============')
withBraces();
要从数组中删除元素,请使用array.filter,因为splice将在原始数组中进行更改.这样写:
case DELETE_INTEREST:
let deleteInterests = state.user.interests;
let newData = deleteInterests.filter(i => i !== action.payload);
return { ...state, user: { ...state.user, interests: newData } };