Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

119
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda