Immutablejs - 使用immutablejs更新深层嵌套树

时间:2015-10-26 21:40:40

标签: javascript reactjs immutability immutable.js

您好我正在尝试更新这个ImmutableJs树,但遇到一些问题也许有人可以帮助我。

以下是代码:

let state = Immutable.Map();
var state1 = state.merge({
    event: {
        days: [
            {
                date: 1,
                sessions: [
                    {
                        id: 1,
                        name: 2,
                        startTime: 1,
                        endTime: 1,
                        description: 1,
                        detailsLink: 1,
                        details: {visible: false}
                    },
                    {
                        id: 2,
                        name: 2,
                        startTime: 2,
                        endTime: 2,
                        description: 2,
                        detailsLink: 2,
                        details: {visible: false}
                    },
                    {
                        id: 3,
                        name: 3,
                        startTime: 3,
                        endTime: 3,
                        description: 3,
                        detailsLink: 3,
                        details: {visible: false}
                    }
                ]
            },
            {
                date: 2,
                sessions: [
                    {
                        id: 1,
                        name: 2,
                        startTime: 1,
                        endTime: 1,
                        description: 1,
                        detailsLink: 1,
                        details: {visible: false}
                    },
                    {
                        id: 2,
                        name: 2,
                        startTime: 2,
                        endTime: 2,
                        description: 2,
                        detailsLink: 2,
                        details: {visible: false}
                    },
                    {
                        id: 3,
                        name: 3,
                        startTime: 3,
                        endTime: 3,
                        description: 3,
                        detailsLink: 3,
                        details: {visible: false}
                    }
                ]
            }
        ]
    }
});

const state2 = state1.setIn(['event','days'], state1.getIn(['event','days']).map(day => {
    return day.get('sessions').map(session => {
        let isVisible = session.get('details').toJS();
        if(!isVisible.visible) {
            return session.setIn(['details','visible'],true);
        }
    })
}))

console.log(state1.toJS());
console.log(state2.toJS());

我可以更新集合。问题是两棵树不再相同了。在第一个中,days键是一个对象,在第二个中,days键是一个数组。

我知道问题在于days.get(' session')返回列表而不是地图。但不确定如何使其发挥作用。

这是代码的jsbin。

https://jsbin.com/mejaxelobe/1/edit?html,js,output

由于

2 个答案:

答案 0 :(得分:1)

听起来你正在用days替换sessions,这可能不是你想要做的。

如果您的目标是在所有会话中设置visible为true,则可以执行以下操作:

const state2 = state1.updateIn(
  ['event','days'],
  days => days.map(
    day => day.update('sessions',
      sessions => sessions.map(
        session => session.updateIn(['details', 'visible'], true)
      )
    )
  )
);

答案 1 :(得分:0)

一个解决方案可能是当你返回时,用“Immutable.Map”包装它?例如,以下??

const state2 = state1.setIn(['event','days'], state1.getIn(['event','days']).map(day => {
    return Immutable.Map(day.get('sessions').map(session => {
        let isVisible = session.get('details').toJS();
        if(!isVisible.visible) {
            return session.setIn(['details','visible'],true);
        }
    }))
}))