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

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

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 Relatório

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 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