CSS Frameworks and Redux
Overview
In this vitamin, you'll build a Shopping Cart application using Redux Toolkit for state management and Bootstrap for styling. This teaches you how modern React applications manage complex state across components.
Why Redux? In our React vitamin, you used useState and useContext for state. That works for small apps, but as apps grow, passing state through many components becomes messy. Redux gives you a single central store that any component can read from or write to.
Why Bootstrap? Instead of writing CSS from scratch, Bootstrap provides pre-built classes like btn btn-primary and card that make your app look professional with minimal effort.
Learning Objectives
- Understand Redux core concepts: Store, Slices, Actions, Reducers
- Use
useSelectorto read state from the Redux store - Use
useDispatchto send actions to update state - Apply Bootstrap classes for responsive, professional styling
Redux Concepts Overview
Store
The store is a single JavaScript object that holds all your application's state. Instead of having state scattered across different components, everything lives in one place. Any component can access it.
Slice
A slice is a collection of Redux logic for a single feature. It contains:
- Initial state - the starting values (e.g.,
{ items: [], totalQuantity: 0 }) - Reducers - functions that update the state
Think of slices as organizing your store into sections. A shopping app might have a cartSlice, userSlice, and productsSlice.
Actions
Actions are plain objects that describe what happened. They have a type (like "cart/addItem") and optionally a payload (the data). You don't create these manually; Redux Toolkit generates them for you.
Reducers
Reducers are functions that take the current state and an action, then return the new state. They define how the state changes in response to actions.
addItem: (state, action) => {
// action.payload contains the data passed when dispatching
state.items.push(action.payload);
}
useSelector
The useSelector hook lets components read data from the store. You use this inside your React components (not in the Redux setup files):
// Inside a component like Cart.js
const items = useSelector((state) => state.cart.items);
The function you pass receives the entire store state and returns the piece you need.
useDispatch
The useDispatch hook lets components send actions to update the store. You also use this inside your React components:
// Inside a component like ProductList.js
const dispatch = useDispatch();
dispatch(addItem({ id: 1, name: "Shirt", price: 25 }));
- Slice, reducers, actions →
store/cartSlice.js(Redux setup) - Store configuration →
store/store.js(Redux setup) - useSelector, useDispatch → Inside your React components (e.g.,
Cart.js,ProductList.js)
The Flow
User clicks "Add to Cart"
↓
Component calls dispatch(addItem(product))
↓
Redux runs the addItem reducer
↓
State updates (item added to cart)
↓
Components using useSelector re-render with new data
Using Bootstrap in React
In React, use className instead of class. Combine multiple classes with spaces:
<button className="btn btn-primary">Click me</button>
<div className="card mt-4">...</div>
Bootstrap Classes Reference
Layout:
container-fluid- Full-width containerrow- Creates a horizontal row for columnscol-md-4,col-md-8- Column widths (out of 12 total)mt-4,mb-3,me-2- Margins (t=top, b=bottom, e=end/right, s=start/left)
Cards:
card- Card containercard-header- Card header sectioncard-body- Card content areacard-title- Card title textcard-img-top- Image at top of card
Buttons:
btn- Base button class (always required)btn-primary- Blue buttonbtn-danger- Red buttonbtn-secondary- Gray buttonbtn-sm- Small button size
Lists:
list-group- Container for list itemslist-group-item- Individual list item
Flexbox:
d-flex- Display as flexboxjustify-content-between- Space items apartalign-items-center- Vertically center items
Navbar:
navbar- Navigation bar containernavbar-dark- Light text for dark backgroundsbg-dark- Dark background colornavbar-brand- Brand/title text
Text:
text-muted- Gray/muted text color
See Bootstrap docs for more.
Part 1: Project Setup
npx create-react-app shopping-cart
cd shopping-cart
npm install @reduxjs/toolkit react-redux bootstrap
Create this file structure:
src/
├── components/
│ ├── ProductList.js
│ ├── Cart.js
│ └── CartItem.js
├── store/