Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

98
Vistas
How do I pass mapped props to a component using useState?

I have a shopping cart component that I've passed a useState prop to update the number of items within the shopping cart / the amount of money that the cart is worth:

import { data } from "./data";

export default function StoreItems({ addToCart, Total, setTotal }) {
  const addToTotal = (data) => {
    addToCart();
    setTotal(Total + data.price);
  };
  return (
          <Grid container spacing={3}>
            {data.map((data) => (
              <Grid item key={data.id} xs={12} sm={6} md={3}>
                <Card>
                  <CardMedia
                    image={data.image}
                  />
                  <CardActions>
                    <div>
                      <Button>Buy</Button>
                      <Button
                        size="small"
                        sx={{ marginLeft: "5px" }}
                        onClick={addToTotal}
                      >
                        Add to Cart
                      </Button>
                    </div>
                  </CardActions>
                </Card>
              </Grid>
            ))}
          </Grid>
  );
}

The Data is a JavaScript array with objects in it that looks like:

export const data = [
  {
    id: 1,
    title: "item 1",
    content: `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam ac aliquam nunc. 
    Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.`,
    image: "https://source.unsplash.com/random/1",
    price: 5000,
  },
];

And the props are passed down from:

function Store() {
  const [cartCount, setCartCount] = useState(0);
  const [Total, setTotal] = useState(0);

  const addToCart = () => {
    setCartCount(cartCount + 1);
  };

  return (
    <>
      <div>
            <StoreItems
              Total={Total}
              setTotal={setTotal}
              addToCart={addToCart}
            />
      </div>
    </>
  );
}

export default Store;

Why is it that I am unable to add the price of data.price to the useState - Total object

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

There's a problem with how you use addToTotal in the StoreItems component. First, the function expects an item of the data array to be passed, but you don't do that explicitly. In your implementation the function gets a click event instead of a data array item. Second, it's better to rename the first argument in map's arrow function because it confuses the data array with its items. Here's how you need to update the component to make things work:

import { data } from "./data";

export default function StoreItems({ addToCart, Total, setTotal }) {
  const addToTotal = (item) => {
    addToCart();
    setTotal(Total + item.price);
  };
  return (
          <Grid container spacing={3}>
            {data.map((item) => (
              <Grid item key={item.id} xs={12} sm={6} md={3}>
                <Card>
                  <CardMedia
                    image={data.image}
                  />
                  <CardActions>
                    <div>
                      <Button>Buy</Button>
                      <Button
                        size="small"
                        sx={{ marginLeft: "5px" }}
                        onClick={() => addToTotal(item)}
                      >
                        Add to Cart
                      </Button>
                    </div>
                  </CardActions>
                </Card>
              </Grid>
            ))}
          </Grid>
  );
}

Here's a simplified playground with the fix: https://stackblitz.com/edit/stackoverflow-71871965?file=src%2FStoreItems.js

about 4 years ago · Juan Pablo Isaza Denunciar

0

First change this : data.map((data) => ....

to this :

 data.map((item) => ....

You need to pass the price to the onClick Function :

 <Button
 size="small"
 sx={{ marginLeft: "5px" }}
 onClick={() => addToTotal(item.price)}>
 Add to Cart
 </Button>

and than change the addToTotal function to this :

const addToTotal = (price) => {
    addToCart();
    setTotal(Total + price);
  };
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda