Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
93 views
in Technique[技术] by (71.8m points)

javascript - How to delete nested element in map Immutable.js

I have the following structure in my redux case:

initialState: SearchState = fromJS({
  isFiltersPanelOpen: false,
  sections: {
    type: {       Course: {
        isSelected: false,
        filterValues: 42,
      },
      Path: {
        isSelected: false,
        filterValues: 12,
      },
      Resources: {
        isSelected: false,
        filterValues: 11,
        }
     }
  }
})

I have to delete Resources. I used deleteIn, but I have a problem. When other cases work, they get the initial value, and my deleted Resources come back to life. If I correctly understand, I should use update/updateIn and then delete/deletIn. I need some examples please.

question from:https://stackoverflow.com/questions/65902726/how-to-delete-nested-element-in-map-immutable-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I am answering blindly here. Without having seen any code, the issue is probably that the modified state is not applied.

Remember, Immutable objects never change, mutations always result in a new object being returned. So if you forget to return/apply the returned object, your change is lost.

const reducer = createReducer(initialState, {
    deleteSection: (state, { id }) => {
        return state.deleteIn(['sections', 'Resources']);
    },
    deleteSectionToo: (state, { id }) => {
        // Update can be used too, but it is mostly useful when you want to change something.
        // e.g. add/remove an item in a sub-list.
        return state.update('sections', (sectionsState) => {
            return sectionsState.delete('Resources');
        });
    }
};

For more examples, consult the docs.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...