Immutable.js如何批量修改对象?

这是组件state

this.state = {
        vcard: Map({
          header: Map({
            width: 0,
            height: 30,
            triangel: Map({
              offset: 6,
              width: 10,
              height: 10
            })
          }),
          repeatHeader: Map({
            width: 0,
            height: 30,
            triangel: Map({
              offset: 5,
              width: 10,
              height: 10
            })
          })
        })
      };

这是修改state值

let vcard = this.state.vcard.setIn(
        ["header"],
        Map({
          width: 0,
          height: 30,
          triangel: Map({
            offset: 6,
            width: 10,
            height: 10
          })
        })
      );
      let newVcard = vcard.setIn(
        ["repeatHeader"],
        Map({
          width: 0,
          height: 30,
          triangel: Map({
            offset: 6,
            width: 10,
            height: 10
          })
        })
      );
      this.setState((state, props) => ({
        vcard: newVcard
      }));

问题就是:setIn我现在是分开设置header和repeatHeader,有没有方法同时在setIn里面进行设置的啊?

阅读 3.8k
2 个回答

merge
作用:浅合并,新数据与旧数据对比,旧数据中不存在的属性直接添加,就数据中已存在的属性用新数据中的覆盖
mergrWith
作用:自定义浅合并,可自行设置某些属性的值
mergeIn
作用:对深层数据进行浅合并
mergeDeep
作用:深合并,新旧数据中同时存在的的属性为新旧数据合并之后的数据
mergeDeepIn
作用:对深层数据进行深合并
mergrDeepWith
作用:自定义深合并,可自行设置某些属性的值
这里用一段示例彻底搞懂merge,此示例为Map结构,List与Map原理相同

this.setState((state, props) => ({
      show: this.state.show.mergeDeep({
        query: false,
        position: !this.state.show.get("position")
      })
    }));

链接:https://www.jianshu.com/p/0fa...

setIn改merge?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题