Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
Using immutability-helper in React, update multiple values in one pass

Can you update two or more values in a nested state using one update method call from immutability-helper?

I tried the code below but only the last line [elementIndex]: {fouls: {$set: 1 }} gets implemented.

this.state={
    players:[{points: 0, fouls: 0, name: 'bob'}, {points: 0, fouls: 0, name: 'joe'}]
}

const element = this.state.players.findIndex(el => el.name === 'bob');

let score = update(this.state.players, {
      [element]: {points: {$set: 2 }},
      [element]: {fouls: {$set: 1 }}
    });

this.setState({ players: score})
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The reason this isn't working is because you can't have duplicate keys in an object (ie two keys [element]).

I believe this will work: change your update statement to:

let score = update(this.state.players, {
  [element]: {points: {$set: 2 }, fouls: {$set: 1 }},
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!