Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change multiple key values at once, and setState in React #980

Closed
jackson-sandland opened this issue Sep 25, 2016 · 2 comments
Closed

Change multiple key values at once, and setState in React #980

jackson-sandland opened this issue Sep 25, 2016 · 2 comments

Comments

@jackson-sandland
Copy link

jackson-sandland commented Sep 25, 2016

@leebyron Thanks for Immutable! I've checked out numerous Stack Overflow questions, tutorials, and blog posts but can't find an answer to this question, which feels like it should be simple to resolve. I have a deeply nested object that looks like this:

let options = fromJS({
      projects:{
        id: 1, 
        selected: true,
        option: "projects",
      },
      collections:{
        id: 2, 
        selected: true,
        option: "collections"
      },
      organizations:{
        id: 3, 
        selected: true,
        option: "organizations"
      }
    })

What I would like to do is change every selected value in this Immutable object to false. I've tried this to no avail:

let options = this.state.options
options.forEach((val, option) {
  let option = option.get(option)
  let falseOptions = options.setIn([option, "selected"], false)
})

this.setState({
  options: falseOptions
})

This does not work as expected. Only the last object in the options object gets it's state changed. What is the best way to update multiple immutable values at once, and then pass those changes to the state?

Thanks in advance!

@jackson-sandland
Copy link
Author

I've sorted it out. The following works beautifully:

let state = this.state.options
    state = state.map((options) => options.set("selected", false))
    this.setState({
      options: state
    })

@leebyron
Copy link
Collaborator

Glad to hear it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants