我以这种方式启动redux
减速器:
export default (state = new Set( [5, 10, 15, 20], action)=>{
...
此后,我应该复制状态:
如果状态是Array
,我会
let newState = [...state];
如果状态是Object
,我会
let newState = {...state};
问题是:当状态为Set
类型时该怎么办?这是进行Set
克隆的简单方法吗?
答案 0 :(得分:1)
根据状态中的一个创建一个新集合:
const state = new Set([5, 10, 15, 20]);
let newState = new Set(state);
// or: let newState = new Set([...state]);
console.log('state:', [...state]);
console.log('new state:', [...newState]);
state.add(11);
console.log('state:', [...state]);
console.log('new state:', [...newState]);
答案 1 :(得分:1)
您可以将要复制的集合传递给新集合的构造函数。 像这样:
let newSet = new Set(state)