From the course: Build Modern Web Apps with React, Hooks, State Management, and APIs Using Vite or Next.js

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Cart slice

Cart slice

Welcome back. Now let's create our cart slice so we can actually perform the cart based operations. Our listing of product is done. We just need to have a cart and need to plug everything together. Inside the source folder, I am going to create a new folder called features. feature, we can have a cart folder, in this we can have a file called cart slice.ts. You can have all the feature-based folders here as your application pursuits. For this cart slice, we'll create an initial state for the cart which will be cart state which we just created and the items will be empty as the cart initially renders. Then we are going to have a constant cart slice and we'll call the create slice function from toolkit. We have to give it a name, we'll say cart, the initial state and the reducers. The first reducer we have to create is add item. It's going to take a state, the action and payload action of type product. We can import it here and we can import the payload action as well. We have to import…

Contents