使用 ImmutableJs 清空数组
Emptying an Array using ImmutableJs
我正在使用 ImmutableJS with Redux and redux-immutable。假设我有一个初始状态,它是一个空的项目数组:
const initialState = fromJS({
items: [],
other: true,
});
我可以添加一个项目:
case ADD_ITEM:
return state
.updateIn(['items'], list => list.concat({id: 1231}))
.set('other', false);
之后,我尝试使用 set
或 updateIn
:
清空项目数组
case EMPTY_ARRAY:
return state
.updateIn(['items'],[])
// or
.set('items', []);
当我尝试使用 state.get('items').toJS()
时出现错误:
items.get(...).toJS is not a function
清空这个嵌套的不可变数组的正确方法是什么?
case EMPTY_ARRAY:
return state
.updateIn(['items'],[]);
应该是
state.set('items', Immutable.List())
您已分配给 属性 简单 Array
,显然没有 .toJS()
方法
另外 update
/updateIn
方法将函数作为第二个参数。
我正在使用 ImmutableJS with Redux and redux-immutable。假设我有一个初始状态,它是一个空的项目数组:
const initialState = fromJS({
items: [],
other: true,
});
我可以添加一个项目:
case ADD_ITEM:
return state
.updateIn(['items'], list => list.concat({id: 1231}))
.set('other', false);
之后,我尝试使用 set
或 updateIn
:
case EMPTY_ARRAY:
return state
.updateIn(['items'],[])
// or
.set('items', []);
当我尝试使用 state.get('items').toJS()
时出现错误:
items.get(...).toJS is not a function
清空这个嵌套的不可变数组的正确方法是什么?
case EMPTY_ARRAY:
return state
.updateIn(['items'],[]);
应该是
state.set('items', Immutable.List())
您已分配给 属性 简单 Array
,显然没有 .toJS()
方法
另外 update
/updateIn
方法将函数作为第二个参数。