小编hea*_*row的帖子

为什么在使用React Redux进行过滤时我的原始状态会发生变化

即时通讯在React应用程序中使用redux。为什么此过滤函数会改变原始state.product?我不明白为什么

state.products = [
   { 
      type: "one",
      products: [
         { active: true },
         { active: false }
     ]
  }
 ]

function mapStateToProps(state) {
 const test = state.products.filter((item) => {
   if(item.type === "one") {
     return item.products = item.products.filter((item) => {
       item.active
      });
    }
    return item;
  });

  return {
    machineSearchWeightRange: state.machineSearchWeightRange,
    filteredItems: test //This will have only products active
 };
}
Run Code Online (Sandbox Code Playgroud)

filteredItems将仅具有活动产品,但state.products也将被更新,仅在尝试再次过滤同一数据时仅包含活动产品。

意见建议

javascript filter reactjs redux

1
推荐指数
1
解决办法
37
查看次数

标签 统计

filter ×1

javascript ×1

reactjs ×1

redux ×1