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

164
Visualizações
How can I update the values of Object.entries?
export const data = [
  {
    size: "S",
    colorMap: { Yellow: 10, Green: 5, Black: 50 },
    productName: "Shirt",
    price: 200
  }
];

I wanted to show the initial values of the colorMapand then update its quantity. How can I update the quantities of the colors which are the values of the Object.entries(colorMap)?

Codesandbox: https://codesandbox.io/s/form-changehanlder-2-2repsp?file=/part2.js

The product here came from the parent component:

This is the child component

import React, { useState } from "react";
import { Grid, TextField } from "@mui/material";

const Part2 = ({ product }) => {
  const [qty, setQty] = useState();

  const handleSubmit = (e) => {
    e.preventDefault();
    console.log(qty);
  };

  return (
    <div>
      {product &&
        product.map((prod, index) => (
          <>
            <Grid item key={index}>
              <form onSubmit={handleSubmit}>
                {Object.entries(prod.colorMap).map((color, index) => (
                  <Grid
                    container
                    rowSpacing={1}
                    columnSpacing={{ xs: 1, sm: 2, md: 3 }}
                    key={color[0]}
                  >
                    <Grid item xs={6}>
                      <TextField
                        type="text"
                        variant="outlined"
                        label="Color"
                        fullWidth
                        value={color[0]}
                        disabled
                      />
                    </Grid>

                    <Grid item xs={6}>
                      <TextField
                        type="number"
                        variant="outlined"
                        fullWidth
                        label="Quantity"
                        value={color[1]}
                        onChange={(e) => console.log(index)}
                      />
                    </Grid>
                  </Grid>
                ))}
              </form>
            </Grid>
          </>
        ))}
    </div>
  );
};

export default Part2;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

First make changeHandler in demo.js as you are using React State in demo.js so you have to make onChangeHandler in that file and pass it in props of part2. Like:

const onChangeValues = (propertyName, index, value) => {
let item = product?.[index];
if (item) {
  item.colorMap[propertyName] = value;
  let prods = [...product];
  prods[index] = item;
  setProduct(prods);
}
};

And pass this function in props of Part2:

 <Grid item>
     <Part2 product={product} onChange={onChangeValues} />
 </Grid>

In part2 Component you can consume it as follows:

 <TextField
     type="number"
     variant="outlined"
     fullWidth
     label="Quantity"
     value={color[1]}
     onChange={({ target: { value } }) => {
        onChangeHandler(color[0], index, value);
     }}
  />

Codesandbox Link: https://codesandbox.io/s/form-changehanlder-2-forked-l9unl0?file=/part2.js:1131-1521

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