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

99
Visualizações
How do I pass the data totalAmount from the child to parent to another child component again?

I have this totalAmount data from the cart.js and how can I pass it to the parent component(app.js) where I will be able to pass it to another child component(info.js). I tried passing it from the Cart.js to the parent, it won't work, this is what it would display:

Identifier 'totalAmount' has already been declared

codesandbox: https://codesandbox.io/s/add-to-cart-sampled-2-with-material-ui-table-pagination-with-reduced-cart-items-firebase-5id6gs?file=/src/Cart.js**strong text**

In the cart.js

const Cart = ({ cartItems, handleCartClearance, handleRemove, handleAdd }) => {

  const totalAmount = cartItems.reduce(
    (price, item) => price + item.quantity * item.price,
    0
  );

  return (
    <div>

    </div>
  );
};

export default Cart;

In the App.js How can I pass the totalAmount to the customerInfo component?

export default function App() {

  return (
    <div className="App">
      <CustomerInfo cartItems={cartItems} /> 
      <br />
          <Cart
            cartItems={cartItems}
            handleCartClearance={handleCartClearance}
            handleRemove={handleRemove}
            handleAdd={handleAdd}
          />
      </Stack>
    </div>
  );
}

CustomerInfo component:

const CustomerInfo = ({ cartItems }) => {
  const handleSubmit = (e) => {
    e.preventDefault();
    console.log(firstName, number, "info");
    console.log(cartItems, "cartItems, info");
    //console the totalAmount here
  };

  return (
    <Container style={{ padding: "12px" }}>

    </Container>
  );
};

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

0

Since totalAmount calcul is only based on cartItems, which is a prop of Cart. I do believe this is not the responsibility of Cart to calcul it but the responsibility of its parent.

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