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

118
Views
Unable manage objects in array independently

I'm working on a text editor using React and I want to keep track of the changes in an array. Whenever I make changes an object is added to the array (as it should) but all the other objects change as well and become the same as the new one. I'm aware of how Javascript doesn't store objects in independent variables if not reassigned so I used the spread operator to create a new array and then add a new object using Object.assign() but it's still not working and I can't figure out what I'm doing wrong.

getNewChangesHistory(update, oldChangesHistory){
  var newChangesHistory = [...oldChangesHistory, Object.assign({}, update)];
  if(newChangesHistory.length > 25){
    delete(newChangesHistory[26]);
  }
  return newChangesHistory;
}

updateDocumentContent(content){
  var newDocument = {...this.state.document};
  newDocument.content = content;

  this.setState(prevState => {return {
    document: newDocument,
    changesHistory: this.getNewChangesHistory(content, prevState.changesHistory),
    hasChanges: true
  }})
}

updateTextbox(editedProperties, key){
  const newDocumentContent = {...this.state.document.content};
  newDocumentContent.textboxes[key] = { //Textboxes are stored as objects of an array
    ...editedProperties,
    id: key
  }
  this.updateDocumentContent(newDocumentContent)
}

render(){
  return(
    <TextBox 
      onEdit={(editedProperties) => {this.updateTextbox(editedProperties, 0)}}
    />
  )
}

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The problem is in updateTextbox. With {...this.state.document.content} it only creates a shallow copy. In this copy the textboxes property will still reference the same object. And you mutate that object by the assignment to its [key] property. So that mutation will be seen in all objects that have that same textboxes object reference.

One way to get rid of this, is to treat textboxes as immutable, and do this:

updateTextbox(editedProperties, key){
  const {content} = this.state.document;
  const newDocumentContent = {
    ...content,
    // create a new textboxes array
    textboxes: Object.assign([], content.textboxes, {
      [key]: {
        ...editedProperties,
        id: key
      }
    })
  };
  this.updateDocumentContent(newDocumentContent);
}
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!