From the course: Master Next.js by Building Scalable Apps with Routing, Databases, and Performance
Unlock this course with a free trial
Join today to access over 25,200 courses taught by industry experts.
Implement cart functionalities - Next.js Tutorial
From the course: Master Next.js by Building Scalable Apps with Routing, Databases, and Performance
Implement cart functionalities
Now, we will implement adding and removing functionality from the cart. For that, I will create a state to handle cart items. So, I will write const cartItems comma setCartItems and will say use state, giving the blank array as the initial state. Now, let me create a function to add the product to the cart. So, I will say const addProductToCart will receive new product and set the product to the state by saying set cart items, previous products, here I will say the spread operator, previous products, comma, new product. I will also create a function to remove products from the cart using the product id. So, I will say const removeProductFromCart will receive product id and update the state using setCartItems, previousProducts, previousProducts.filter will receive the product and we will return the rest of the products. Now I will pass all of these as a value to the provider by writing value cart items comma add product to cart, remove product from cart and set cart items. I'll also…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
-
-
(Locked)
Initiating client section1m 53s
-
(Locked)
Creating header component5m 20s
-
(Locked)
Implementing search bar2m 16s
-
(Locked)
Joining Tailwind classes with ideal approach2m 34s
-
(Locked)
RQ: Joining Tailwind classes with ideal approach2m 38s
-
(Locked)
Adding profile drop-down4m 31s
-
(Locked)
useRef() hook4m 42s
-
(Locked)
Close profile drop-down menu on outside click3m 2s
-
(Locked)
RQ: Close profile drop-down menu on outside click2m 36s
-
(Locked)
Creating input component1m 54s
-
(Locked)
Creating home page2m 34s
-
(Locked)
Creating filters section6m 15s
-
(Locked)
Creating custom accordion component10m 58s
-
(Locked)
RQ: Creating custom accordion component1m 37s
-
(Locked)
Completing custom accordion component4m 35s
-
(Locked)
Continue on filter section3m 2s
-
(Locked)
Creating price range slider component4m 39s
-
(Locked)
Creating product card component8m 17s
-
(Locked)
Completing product card component2m 47s
-
(Locked)
Creating button component1m 44s
-
(Locked)
API creation for product listing3m 59s
-
(Locked)
Dynamic product rendering using server action7m 12s
-
(Locked)
API creation for fetching product types1m 41s
-
(Locked)
Dynamic product type rendering using API2m 28s
-
(Locked)
Set filter values into the search params4m 29s
-
(Locked)
Modifying products API to apply filters5m 35s
-
(Locked)
Filter functionality on client side3m 44s
-
(Locked)
Search filter functionality4m 31s
-
(Locked)
Creating product page6m 38s
-
(Locked)
Completing product page3m 21s
-
(Locked)
API creation for fetching a product by its ID2m 54s
-
(Locked)
Dynamic product details rendering using API3m 25s
-
(Locked)
Rendering products based on status2m 42s
-
(Locked)
Creating cart page3m 24s
-
(Locked)
Rendering cart item card6m 22s
-
(Locked)
Completing the cart UI4m 55s
-
(Locked)
Creating context for handling products1m 50s
-
(Locked)
RQ: Creating context for handling products1m 28s
-
(Locked)
Implement cart functionalities8m 4s
-
(Locked)
Making cart page dynamic2m 54s
-
(Locked)
Enabling quantity selection3m 45s
-
(Locked)
Finishing dynamic cart page5m 6s
-
(Locked)
-
-
-
-