Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

120
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda