In last part we built the Store. But your React components have no idea it exists.
A component can't just reach into the store directly. React and Redux are two separate worlds. Something has to connect them. That's what this article is about the bridge.
Two things need to happen. The store needs to be injected into the React tree. And every component that talks to Redux needs a safe, typed way to do it.
Why Not Just Use the Raw Hooks?
React-Redux ships with useSelector and useDispatch. They work out of the box.
But they're untyped.
useDispatch() returns a generic dispatch function. useSelector doesn't know what your state looks like. Every component would need to import RootState and AppDispatch and wire the types manually, every single time. That's repetition.
Instead, we create typed wrappers once in hooks.ts and then every component will import from there.
Create a file name hooks.ts under store folder and add following code.
import { useDispatch, useSelector, type TypedUseSelectorHook } from "react-redux";
import { AppDispatch, RootState } from "./store";
type DispatchFunc = () => AppDispatch;
export const useCartDispatch: DispatchFunc = useDispatch;
export const useCartSelector: TypedUseSelectorHook<RootState> = useSelector;we saw in last article that store exports, RootState and AppDispatch, where RootState is for reading and AppDispatch is for writing, now we're simply wrapping the raw hooks with our own types so every component gets type safety for free.
useCartDispatch for writing.
useCartSelector for reading.
TypedUseSelectorHook comes from React-Redux. It's a generic that wraps useSelector with your specific state type.
Check out the full implementation and give the repo a star on redux-simplified
The UI Shell
The store is wired. The hooks exist. And we need to place the Provider is in place. This is the right moment to look at what's waiting on the UI side. Redux is the goal. But we're building a real app, so there are components too. As per our structure we are done with store, now go ahead and create folder named "components" and we will add components one by one.
src/
├── App.tsx
├── dummy-products.ts
├── components/
│ ├── Header.tsx
│ ├── Product.tsx
│ ├── Cart.tsx
│ ├── CartItems.tsx
│ └── Shop.tsx
└── store/
├── store.ts
├── cart-slice.ts
└── hooks.tsYou don't need to master these right now. This is just orientation, a map of the UI so nothing surprises you in later articles. Each component is a placeholder today. Each one will come alive as we wire in Redux. Let's start with Index.html.
1. Index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Redux + Typescript</title>
</head>
<body>
<div id="modal"></div>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>Two Divs That Matter
<div id="modal"></div>
<div id="root"></div>There are two divs. root is standard, React mounts your entire app here. But modal is separate, sitting above it in the DOM. This is where the cart dialog will render. Not inside the component tree. Above it.
2. Cart.tsx: The Portal
import { createPortal } from 'react-dom';
import CartItems from './CartItems.tsx';
type CartProps = {
onClose: () => void;
};
export default function Cart({ onClose }: CartProps) {
return createPortal(
<>
<div className="cart-backdrop" />
<dialog id="cart-modal" open>
<h2>Your Cart</h2>
<CartItems />
<p id="cart-actions">
<button onClick={onClose}>Close</button>
</p>
</dialog>
</>,
document.getElementById('modal')!
);
}The cart renders as a <dialog>a proper HTML modal element. But notice the second argument to createPortal: document.getElementById('modal'). That's the div, I was referring to in index.html.
Why does this matter? If the cart rendered inside the normal component tree, it would inherit CSS stacking contexts from its parents. Overlays would break. By portaling to #modal which sits outside #root entirely the cart floats above everything, without CSS battles.
onClose is a prop for now. Later, it won't need to be. The store will handle visibility state.
3. CartItems.tsx: Commented On Purpose
export default function CartItems() {
return (
<></>
// <div id="cart">
// {
// cartItems.length === 0 &&
// <p>No items in cart!</p>
// }
// {cartItems.length > 0 && (
// <ul id="cart-items">
// {[cartItems.map](http://cartItems.map)((item) => {
// const formattedPrice = $${item.price.toFixed(2)};
// return (
// <li key={[item.id](http://item.id)}>
// <div>
// <span>{item.title}</span>
// <span> ({formattedPrice})</span>
// </div>
// <div className="cart-item-actions">
// <button onClick={() => handleRemoveFromCart([item.id](http://item.id))}>
// -
// </button>
// <span>{item.quantity}</span>
// <button onClick={() => handleAddToCart(item)}>+</button>
// </div>
// </li>
// );
// })}
// </ul>
// )}
// <p id="cart-total-price">
// Cart Total: <strong>{formattedTotalPrice}</strong>
// </p>
// </div>
);
}This component is intentionally empty right now. The commented code inside is the finished cart, items, quantities, add/remove buttons, total price. It's all there, waiting. But every line of that logic depends on Redux hooks that we yet to map.
4. Header.tsx: The Cart Button That Does Nothing (Yet)
import Cart from './Cart.tsx';
export default function Header() {
function handleOpenCartClick() {}
return (
<>
<header id="main-header">
<div id="main-title">
<h1>learn redux + Typescript with cart feature</h1>
</div>
<p>
<button onClick={handleOpenCartClick}>Cart ({0})</button>
</p>
</header>
</>
);
}Two things to notice.
handleOpenCartClick is empty. The button renders. Nothing opens. That's temporary.
Cart ({0}) the count is hardcoded to zero. That zero will eventually be replaced by a selector reading from the store. When it is, the header will always reflect the real cart count. Automatically. Without any prop passing.

Join the conversation! Your thoughts help the community grow.