₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,009 members, 8,419,900 topics. Date: Thursday, 04 June 2026 at 06:56 AM

Toggle theme

Help On My React Project - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingHelp On My React Project (1020 Views)

1 Reply (Go Down)

Help On My React Project by Laryfrosh(op): 11:06am On Jul 08, 2022
Good Morning Devs

I have been trying to build my first project on react (a mini e-commerce app) and I am currently faced with a challenge which I have been trying to resolve for over 2 weeks now.

My challenge is summing up the total on the cart after pushing the items to the cart.

Here is the link to code on GitHub.

https://github.com/Hilary017/hillsMart

Please I will be glad if anyone can assist me on how I can implement this.

Thanks
Re: Help On My React Project by jbreezy: 11:12am On Jul 08, 2022
Laryfrosh:
Good Morning Devs

I have been trying to build my first project on react (a mini e-commerce app) and I am currently faced with a challenge which I have been trying to resolve for over 2 weeks now.

My challenge is summing up the total on the cart after pushing the items to the cart.

Here is the link to code on GitHub.

https://github.com/Hilary017/hillsMart

Please I will be glad if anyone can assist me on how I can implement this.

Thanks
Get them in an array, and use the .reduce() method. It will sum every prices inside the array for you.
Re: Help On My React Project by Laryfrosh(op): 11:19am On Jul 08, 2022
jbreezy:
Get then in an array, and use the .reduce() method. It will sum every prices inside the array for you.
I have tried that but I couldn't get them in an array.
Guess that has got something to do with the structure of the code but I have not been able to figure out a better way to implement it...

Please, can you kindly take a look at the code?
Re: Help On My React Project by Nobody: 11:21am On Jul 08, 2022
Array.reduce should help you.

Example.

let numbers = [1, 2, 3];
let sum = numbers.reduce(function (previousValue, currentValue) {
return previousValue + currentValue;
});

console.log(sum);



But I'm assuming your cart item object also has a quantity.

So you wanna sum up all the Cart item quantities using array.reduce()

For easy readability you may want to also save that in a state.
Re: Help On My React Project by Extratyre01(m):
Laryfrosh:
Good Morning Devs

I have been trying to build my first project on react (a mini e-commerce app) and I am currently faced with a challenge which I have been trying to resolve for over 2 weeks now.

My challenge is summing up the total on the cart after pushing the items to the cart.

Here is the link to code on GitHub.

https://github.com/Hilary017/hillsMart

Please I will be glad if anyone can assist me on how I can implement this.

Thanks
Try to mail me here
Re: Help On My React Project by jbreezy: 11:44am On Jul 08, 2022
Laryfrosh:
I have tried that but I couldn't get them in an array.
Guess that has got something to do with the structure of the code but I have not been able to figure out a better way to implement it...

Please, can you kindly take a look at the code?
Screenshot and let me take a look.
Re: Help On My React Project by Laryfrosh(op): 11:59am On Jul 08, 2022
jbreezy:
Screenshot and let me take a look.
The files are kinda much... I can send the GitHub link
Re: Help On My React Project by jbreezy: 12:01pm On Jul 08, 2022
Laryfrosh:
The files are kinda much... I can send the GitHub link
Just the part you used the reduce() method.
Re: Help On My React Project by Laryfrosh(op): 12:38pm On Jul 08, 2022
jbreezy:
Just the part you used the reduce() method.
This is where my problem is... I have not been able to arrange the individual total into an array
Re: Help On My React Project by chukwuebuka65(m):
Laryfrosh:
Good Morning Devs

I have been trying to build my first project on react (a mini e-commerce app) and I am currently faced with a challenge which I have been trying to resolve for over 2 weeks now.

My challenge is summing up the total on the cart after pushing the items to the cart.

Here is the link to code on GitHub.





https://github.com/Hilary017/hillsMart

Please I will be glad if anyone can assist me on how I can implement this.

Thanks
updated: u need array that contains ur individual item price total in ur usecontext.

Const [totalcartprice, setTotalcartprice] = useState([])

Then below ur ( const total = props.price * quantity)

Do this: setTotalprice( prices => [•••prices, total] )

Totalcartprice now contains an array of all ur individual total prices. Goodluck
Re: Help On My React Project by Nobody: 1:42pm On Jul 08, 2022
Laryfrosh:
Good Morning Devs

I have been trying to build my first project on react (a mini e-commerce app) and I am currently faced with a challenge which I have been trying to resolve for over 2 weeks now.

My challenge is summing up the total on the cart after pushing the items to the cart.

Here is the link to code on GitHub.

https://github.com/Hilary017/hillsMart

Please I will be glad if anyone can assist me on how I can implement this.

Thanks
Hmm op, be like we're working on the same project, did you get the project from frontend mentors ?

Re: Help On My React Project by Laryfrosh(op): 7:51pm On Jul 08, 2022
chukwuebuka65:
updated: u need array that contains ur individual item price total in ur usecontext.

Const [totalcartprice, setTotalcartprice] = useState([])

Then below ur ( const total = props.price * quantity)

Do this: setTotalprice( prices => [•••prices, total] )

Totalcartprice now contains an array of all ur individual total prices. Goodluck
Thanks...

I will try it out
Re: Help On My React Project by Laryfrosh(op): 7:52pm On Jul 08, 2022
Think4Myself:
Hmm op, be like we're working on the same project, did you get the project from frontend mentors ?
No o... I just brought this one up from my head... LoL
Re: Help On My React Project by ahmedsaniadamu(m): 11:00pm On Jul 08, 2022
hope your products are array of objects? if so then e.g products = [ { name:"", price : 20 }, { name:"", price : 50 }, { name:"", price : 80 } ]
const total = product.reduce ( ( product , initialValue ) => { return product.price + initialValue } , 0 //initial value. )
console.log( total) // = 150
Re: Help On My React Project by Phantaminum1(f): 7:27am On Jul 09, 2022
Laryfrosh:
Good Morning Devs

I have been trying to build my first project on react (a mini e-commerce app) and I am currently faced with a challenge which I have been trying to resolve for over 2 weeks now.

My challenge is summing up the total on the cart after pushing the items to the cart.

Here is the link to code on GitHub.

https://github.com/Hilary017/hillsMart

Please I will be glad if anyone can assist me on how I can implement this.

Thanks
I just checked your totalCost state and it is empty.

Or send the link to the file you want implement the total price.

I just finished a similar project, do I might be able to help.
1 Reply

Help Needed On React ProjectMy New React ProjectMy React Single Page For Small Start Up Business (Request Web App - No Backend )234

Google Now The 5th Biggest In The Us. Bigger Than The Biggest Bank In The WorldSap HrDoes Anybody Have The Knowledge Of Opencart ? Please Help Me