简体   繁体   English

使用ImmutableJs清空数组

[英]Emptying an Array using ImmutableJs

I'm using ImmutableJS with Redux and redux-immutable . 我在Reduxredux-immutable中使用ImmutableJS Suppose I have an initial state which is an empty array of items: 假设我有一个初始状态,它是一个空数组:

const initialState = fromJS({
  items: [],
  other: true,
});

I can than add an item: 我可以添加一个项目:

case ADD_ITEM:
      return state
      .updateIn(['items'], list => list.concat({id: 1231}))
      .set('other', false);

Afterwards I try to empty the array of items using set or updateIn : 之后我尝试使用setupdateIn清空项目数组:

case EMPTY_ARRAY:
          return state
          .updateIn(['items'],[])
          // or
          .set('items', []);

When I try to use a state.get('items').toJS() I get an error: 当我尝试使用state.get('items').toJS()出现错误:

items.get(...).toJS is not a function

What is the correct way to empty this nested immutable array? 清空这个嵌套的不可变数组的正确方法是什么?

case EMPTY_ARRAY:
      return state
      .updateIn(['items'],[]);

should be 应该

state.set('items', Immutable.List())

You've assigned to property simple Array which obviously doesn't have .toJS() method 你已经分配了属性simple Array ,它显然没有.toJS()方法

Also update / updateIn methods takes a function as a second argument. update / updateIn方法也将函数作为第二个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM