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

403
Views
React Typescript: Two different States are Updating

I have two different States from the same class.

For some reason, both get updated through this.setState({selectedRecord: newValue}):

selectedRecord: ModelClass,
question: Array<ModelClass>,

The state question is used in a Table for rendering all data, and the state selectedRecord is a copy of the selected Row, which is used to modify the Row via an Form.

<Button type="primary" icon={<EditOutlined />} onClick={() => {
      this.setState({ selectedRecord: record }, () => {
      this.setState({ modalVisible: true });
   });
}} />

So in the Form I modify the state selectedRecord via Input, Checkbox... and complete it with an setState, but the problem is that the changes are also mutate the question State.

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

your dropdownItems has been mutated, try this to retrieve data from your dropdownItems Temp.group = [...this.state.dropdownItems][index].group

about 4 years ago · Santiago Trujillo Report

0

When you do this:

let Temp = this.state.selectedRecord;
Temp.group = this.state.dropdownItems[index].group;
Temp.free = this.state.dropdownItems[index].free;

...you actually modify the properties (mutate) your previously selected record. Therefore you now make it look like your next selected one. And it affects your question state if it contains a reference to that previous record.

It is unclear why you seem to need to retrieve your previously selected record in the first place, rather than just setting the next one with e.g.:

// No need for Temp at all
this.setState({ selectedRecord: this.state.dropdownItems[index] });
about 4 years ago · Santiago Trujillo 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!