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

137
Visualizações
Pushing objects in an array only returns last object pushed in react

I Know this question to be duplicated, but none of the other answers worked for me... I am trying to push objects to my array using the onChnage method. But every time my array returns the last value, not the full value. I tried so many times to fix this issue. But I can't.

function addVariantSection(data) {
    const handleChecked = (id) => (e) => {
      const tempData1=[];
      const { checkedID } = e.target;
      const vID = e.currentTarget.id.split('-', 1);
      tempData1.push({ productId:itemID, variantId:vID})
      setChecked((values) => ({ ...values, [id]: checkedID, }));
      setVariant([...tempData1]); // variant always give the last value.
    };
    return (
      <Grid key={data._id} container spacing={10}>
        <Grid item xs={12} style={{ justifyContent: 'space-between', display: 'flex' }}>
          <div>{`${data.variant1}-${data.variant2}`}</div>
          {/* eslint-disable-next-line no-useless-concat */}
          <Checkbox id={`${data._id}-` + `checkData`} color="primary" checked={checked[data._id]} onChange={handleChecked(data._id)} />
        </Grid>
      </Grid>
    );
  }
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You are overwriting setVariant with the array - tempData1. So you need to preserve the previous value of setVariant to get the complete array you are expecting.

So do this instead

setVariant((prev) => ([...prev, ...tempData1]));

Your function will look like this:

const handleChecked = (id) => (e) => {
    const tempData1=[];
    const { checkedID } = e.target;
    const vID = e.currentTarget.id.split('-', 1);
    tempData1.push({ productId:itemID, variantId:vID})
    setChecked((values) => ({ ...values, [id]: checkedID, }));
    setVariant((prev) => ([...prev, ...tempData1]));
};
about 4 years ago · Juan Pablo Isaza Relatório

0

You are simply setting the value of variant as a new array here, which is going to have tempData1:

setVariant([...tempData1]);

Firstly you need the previous value of variant, so the callback pattern for setState is going to be useful. It gives a reliable value for the previous state always. And using spread ..., simply append the new object at the end.

const handleChecked = (id) => (e) => {
      const { checkedID } = e.target;
      const vID = e.currentTarget.id.split('-', 1);
      setChecked((values) => ({ ...values, [id]: checkedID, }));
      setVariant((variant) => { return [...variant,{ productId:itemID, variantId:vID}]}); // variant always give the last value.
    };
about 4 years ago · Juan Pablo Isaza Relatório

0

The answers above should be ok, but if you don't want the format where you put a function inside a setState what you can do is:

const [variant, setVariant] = useState([]);

.
.
.

setVariant([...variant, ...tempData1]);
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