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

481
Views
How can I total up multiple JSX prices from inside a return()?

I have a component in React that recieves a cat array containing multiple objects. In each of these objects, there is a .price attribute.

What I'm hoping to do is total all of these prices up and print them once within this same component.

Here's what I tried:

const Basket = ({basket}) =>{
    return(

        <div className = "basketWrapper">
            <div className = "paymentWrapper">
            let currentTotal = 0;
                {basket.map((cat, index) => {
                    
                    currentTotal += Math.floor(cat.price);
                    return(

                        <div className = "catsIN">
                            <h2>Cat: {cat.name}</h2>
                            <p>{cat.name} is: £{cat.price}</p>
                        </div>
                    )
                })
                    
                }
                    <div>
                        <h4>Your total is: £{currentTotal}</h4>
                    </div>               
            </div>
        </div>
    );
}

export default Basket;

However, when I run this, it returns an error that currentTotal is not defined. I get that it's outside of the map functions scope, but Im not sure how to achieve this at the moment. Just been working with react for a short while.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Any code that you want to execute as part of your JSX should be in {} and the let currentTotal = 0; is not part of a code section.

You should calculate it before, like this:

const Basket = ({basket}) =>{
    const total = basket.reduce((prev, cur) => prev + cur.price, 0);
    return(

        <div className = "basketWrapper">
            <div className = "paymentWrapper">
                {basket.map((cat, index) => {
                    
                    currentTotal += Math.floor(cat.price);
                    return(

                        <div className = "catsIN">
                            <h2>Cat: {cat.name}</h2>
                            <p>{cat.name} is: £{cat.price}</p>
                        </div>
                    )
                })}
                <div>
                   <h4>Your total is: £{total}</h4>
                </div>               
            </div>
        </div>
    );
}

export default Basket;
about 4 years ago · Juan Pablo Isaza Report

0

Your code is never actually setting the currentTotal variable as it is in your dom.

Also, I would use the reduce function to tally up the total price, here are the changes to the code to presumably make it work.

const Basket = ({basket}) =>{
    let currentTotal = basket.reduce((c, t) => Math.floor(c.price) + t, 0 );
    return(

        <div className = "basketWrapper">
            <div className = "paymentWrapper">
            
                {basket.map((cat, index) => {
                    return(

                        <div className = "catsIN">
                            <h2>Cat: {cat.name}</h2>
                            <p>{cat.name} is: £{cat.price}</p>
                        </div>
                    )
                })
                    
                }
                    <div>
                        <h4>Your total is: £{currentTotal}</h4>
                    </div>               
            </div>
        </div>
    );
}

export default Basket;
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!