Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

92
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!