Shopping Cart with Redux Toolkit
Part of the MERN Stack Roadmap
In short
This project ties together everything in the phase: a product catalogue loaded with RTK Query and a cart managed with a Redux Toolkit slice. It is a small version of what Flipkart or Myntra do on every page.
Build a product catalogue with a cart managed by Redux Toolkit and product data fetched with RTK Query, including quantities, totals in rupees and persistence.

Think of it like this
The kirana store register
In a busy kirana store, any helper can add items to a customer's bill, but there is one register at the counter. Everyone writes to it through fixed actions: add, remove, change quantity. The Redux store is that one register, and the slice reducers are the rules for writing in it.
Watch it happen
Adding a product to the cart
click Add
1/4Ananya taps Add to cart on a ₹1,299 laptop bag.
The big ideas
What you need to know
Plan the slices
A cart slice for items and quantities, plus an RTK Query api slice for products. Server data stays out of the cart slice.
Derive totals with selectors
Don't store the total. Compute it in a selector from items, so it can never be out of sync.
Persist the cart
Save the cart to localStorage with store.subscribe and load it as the initial state, so a refresh doesn't empty it.
Prices come from the server
The cart shows prices, but the server must recalculate the order total at checkout. Never trust the client's total.
How it works
Build order
- 1
Products API
Create an Express route GET /api/products that returns products from MongoDB.
- 2
RTK Query
Define productsApi with a getProducts endpoint and use useGetProductsQuery in the catalogue.
- 3
Cart slice
createSlice with addToCart, removeFromCart, setQuantity and clearCart.
- 4
Selectors and UI
selectCartCount and selectCartTotal feed the header badge and the cart page.
- 5
Persist and polish
Save to localStorage, add empty and loading states, and format prices in rupees.
See it in code
Walk through the code
1// src/features/cart/cartSlice.js2import { createSlice } from "@reduxjs/toolkit";3 4const cartSlice = createSlice({5 name: "cart",6 initialState: { items: [] }, // { _id, name, price, qty }7 reducers: {8 addToCart(state, action) {9 const existing = state.items.find((i) => i._id === action.payload._id);10 if (existing) existing.qty += 1;11 else state.items.push({ ...action.payload, qty: 1 });12 },13 setQuantity(state, action) {14 const { id, qty } = action.payload;15 const item = state.items.find((i) => i._id === id);16 if (item) item.qty = Math.max(1, qty);17 },18 removeFromCart(state, action) {19 state.items = state.items.filter((i) => i._id !== action.payload);20 },21 clearCart(state) {22 state.items = [];23 },24 },25});26 27export const { addToCart, setQuantity, removeFromCart, clearCart } = cartSlice.actions;28export const selectCartCount = (state) => state.cart.items.reduce((n, i) => n + i.qty, 0);29export const selectCartTotal = (state) => state.cart.items.reduce((sum, i) => sum + i.price * i.qty, 0);30export default cartSlice.reducer;1/5createSlice sets the slice name and its starting state.
The cart slice. The store combines it with the RTK Query productsApi reducer and middleware.
Save yourself the pain
Common mistakes
Oops
Copying the products list from RTK Query into the cart slice.
Do this instead
Let RTK Query own server data. The cart stores only what the user chose: ids, names, prices and quantities.
Oops
Storing total in state and updating it in every reducer.
Do this instead
Compute it with a selector. Stored totals drift when one reducer forgets to update them.
Oops
Sending the cart total to the server and charging that amount.
Do this instead
Send item ids and quantities. The server looks up current prices and computes the total.
Your mission
Ship the cart
Quick check
Why is it safe to write existing.qty += 1 inside a createSlice reducer?
Go deeper
- 1Show a cart badge in the navbar and a cart page with totals and GST.
- 2Persist the cart so it survives a refresh.
- 3Write one reducer test for adding the same product twice.
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.