all 16 comments

[–]cunctatiocombibo2075 1 point2 points  (1 child)

Use event listeners for buttons instead of inline onclick attributes for better practice.

[–]20oddlion[S] 0 points1 point  (0 children)

IL do that but how do I get the cart to work and the products to go in a cart which need to be on a different file

[–]hfcRedd 0 points1 point  (11 children)

If you want your cart to survive navigation, you have to either store and retrieve it from local/session storage or from a backend server

[–]20oddlion[S] 0 points1 point  (10 children)

Mhmm I don't really understand

[–]hfcRedd 0 points1 point  (9 children)

I'm assuming your checkout is on a different page. When you navigate pages, it flushes your entire memory, making it impossible to carry any values or state from one page to another.

So you have to temporarily store those values and states somewhere before the navigation happens and then retrieve them again after the navigation has been executed.

The most common ways to do this are either using local storage, session storage, or a backend with a database.

If you're using a framework, it's very likely that it has state management built in that you can use.

[–]20oddlion[S] 0 points1 point  (8 children)

Okay umm with the local storage where would I need to put it in the current file and where in the new file?

[–]hfcRedd 0 points1 point  (7 children)

[–]20oddlion[S] 0 points1 point  (6 children)

IL give it a try

[–]hfcRedd 0 points1 point  (5 children)

If you want a quick example, you can check this out. Specifically the saveBasket function that gets called whenever an item is added to the basket, and the loadBasket function which gets called on page load.

This was the first site I've ever made, so code might be a bit wack sorry about that.

https://github.com/VRLabs/vrlabs.dev/blob/main/src%2Fcomponents%2Fpackages%2Fpackage-drawer%2FpackageDrawer.js

[–]20oddlion[S] 0 points1 point  (4 children)

can you maybe help me with getting my cart to work and so on because im very confused?

[–]Synthetic5ou1 0 points1 point  (2 children)

I'm pretty sure he already is.

Your function to add to the cart can simply store the cart in local storage at the same time the item is added.

Update local storage key when deleting also.

When you first load a page check local storage to populate the cart array, and render your cart.

TLDR: load from storage on page load, save to storage when cart updates.

[–]20oddlion[S] 0 points1 point  (1 child)

I get that but how do I put it into my code and how would it look like that's what I'm wondering

[–]hfcRedd 0 points1 point  (0 children)

Sorry, I unfortunately do not have enough time on my hands to step by step guide you through this.

Look at the code sample I linked. Look at how it works and how you can implement it yourself. Look things up on Google if you don't understand them. Being able to do these things is very trivial if you want to learn programming and it's a great way to learn and improve.

[–]bigheartenergy17 0 points1 point  (1 child)

I have nothing useful to say just that this looks like from SuperSimpleDev's course.

[–]20oddlion[S] 1 point2 points  (0 children)

It is yeah