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

138
Visualizações
Unwanted modification in State variable (Hook), due to manipulation of local variable
import React, { useState, useEffect } from "react";

export const Test = () => {
  const [data, setData] = useState([]);
  const file = [{ id: 1, description: "Test Data" }];

  useEffect(() => setData(file), []);

  const manipulateData = (data) => {
    let tempArray = [...data];
    tempArray.map((item) => delete item.id);
  };

  return (
    <>
      <h1>Testing Data</h1>
      {manipulateData(data)}
      {console.log(data)}
    </>
  );
};

I want to make modification to the local variable "tempArray", and the changes shall not reflect in the variable "data"

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Your way is right in case you are not using a multi-dimensional array, but here you are cloning a multi-dimensional array, and by using the = operator, it’ll assign objects/arrays by reference instead of by value! That means whenever you modify any of the two objects it's going to affect the value in both.

A good way to solve this is by creating a deep clone by doing something like this

    const tempArray = JSON.parse(JSON.stringify([...data]))
    tempArray.map((item) => delete item.id)
about 4 years ago · Juan Pablo Isaza Relatório

0

This happens because you are creating a new array, but the objects in that array are not deeply copied. Instead, they represent just a reference to the very same objects as in data.

Because both references are pointing at the same object, if you delete an object property from the tempArray objects, you are actually deleting it from the data objects.

If you want to solve this, you have to destroy the reference by making a deep copy of the objects from the data array.

Here is an example how to do it:

export const Test = () => {
  const [data, setData] = useState([{ id: 1, description: "Test Data" }]);

  const manipulateData = (data) => {
    let tempArray = data.map((item) => {item.description}); //this will create an array with new object, and destroy the reference to the objects of data.
  };

  return (
    <>
      <h1>Testing Data</h1>
      {manipulateData(data)}
      {console.log(data)}
    </>
  );
};

Also, instead of deleting a property, better pick the one that you need.

about 4 years ago · Juan Pablo Isaza Relatório

0

The following code:

let tempArray = [...data];

only shallow copies your data array, meaning that any of the objects inside of tempArray refer to the same objects in memory that are within the data array. This means that when you loop over your objects with .map(), each item refers to an object from data as well, which results in the objects in data changing when you use delete item.id.

Currently, you are using .map() just to loop over your tempArray, this isn't the purpose of .map(). The .map() method returns a new array, and the callback function you pass it should return a new value that you want to map the current value to. This means you can use .map() to create tempArray, by making the callback function return a new object that excludes the id property:

const manipulateData = (data) => {
  const tempArray = data.map(({id, ...rest}) => rest);
  // ... do what you need with `tempArray` ...
};

Above, {id, ...rest} is destructuring assignment syntax, which extracts the id property from the current element, and stores all other properties/values from the current object in rest. This allows you to create a new object that excludes the id property.

As a side note, you shouldn't be calling methods such as console.log() and manipulateData() inside of JSX if they don't return anything meaningful that will be rendered to the screen, instead, you can put these in a useEffect() hook.

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