Effects & Data Lesson 12 of 13
React — Sharing State Between Components
Share data between components by lifting state up to a common parent, pass setter functions as props, avoid prop drilling with Context, and organize state well.
When two components need the same data, like a product list and a cart total, the state can't live inside just one of them. The solution is to lift state up to their closest common parent and pass it down as props.
Lifting state up
<div id="root"></div>
<script src="/vendor/react.min.js"></script>
<script src="/vendor/react-dom.min.js"></script>
<script src="/vendor/babel.min.js"></script>
<style>
body { font-family: sans-serif; }
.wrap { display: flex; gap: 16px; padding: 12px; flex-wrap: wrap; }
.box { border: 1px solid #ddd; border-radius: 10px; padding: 10px; min-width: 180px; }
</style>
<script type="text/babel">
const { useState } = React;
const products = [
{ id: 1, name: "Pandesal", price: 25 },
{ id: 2, name: "Kape", price: 8 },
{ id: 3, name: "Itlog", price: 9 },
];
function ProductList({ onAdd }) {
return (
<div className="box">
<h3>Products</h3>
{products.map((p) => (
<p key={p.id}>{p.name} ₱{p.price} <button onClick={() => onAdd(p)}>Add</button></p>
))}
</div>
);
}
function Cart({ items, onClear }) {
const total = items.reduce((sum, p) => sum + p.price, 0);
return (
<div className="box">
<h3>Cart ({items.length})</h3>
<ul>{items.map((p, i) => <li key={i}>{p.name}</li>)}</ul>
<strong>Total: ₱{total}</strong>
{items.length > 0 && <p><button onClick={onClear}>Clear</button></p>}
</div>
);
}
function App() {
const [cart, setCart] = useState([]); // state lives in the parent
return (
<div className="wrap">
<ProductList onAdd={(p) => setCart([...cart, p])} />
<Cart items={cart} onClear={() => setCart([])} />
</div>
);
}
ReactDOM.createRoot(document.getElementById("root")).render(<App />);
</script>
cartstate lives inApp, the common parent.ProductListgets a function (onAdd) to request a change.Cartgets the data (items) to display it.
This is React's one-way data flow: data goes down through props; requests to change it go up through callback functions.
Where should state live?
- List the components that use the data.
- Find their closest common parent.
- Put the state there, and pass it down.
Keep state as low as possible: if only one component uses it, keep it there.
Prop drilling and Context
When data must pass through many layers of components that don't use it (like the logged-in user or a theme), passing props gets tedious. This is called prop drilling. React's Context lets deep components read a value directly:
<div id="root"></div>
<script src="/vendor/react.min.js"></script>
<script src="/vendor/react-dom.min.js"></script>
<script src="/vendor/babel.min.js"></script>
<script type="text/babel">
const { useState, useContext, createContext } = React;
const ThemeContext = createContext("light");
function Card() {
const theme = useContext(ThemeContext); // read without props
const style = theme === "dark"
? { background: "#1d2321", color: "#e8efec" }
: { background: "#fff", color: "#1d2321" };
return <div style={{ ...style, padding: 16, borderRadius: 10, border: "1px solid #ccc" }}>Theme: {theme}</div>;
}
function Page() {
return <section><Card /></section>; // Page doesn't need to know about theme
}
function App() {
const [theme, setTheme] = useState("light");
return (
<ThemeContext.Provider value={theme}>
<div style={{ fontFamily: "sans-serif", padding: 12 }}>
<button onClick={() => setTheme(theme === "light" ? "dark" : "light")}>Toggle theme</button>
<Page />
</div>
</ThemeContext.Provider>
);
}
ReactDOM.createRoot(document.getElementById("root")).render(<App />);
</script>
Use Context for values many components need (user, theme, language). For most other data, plain props keep things simpler and easier to follow.
State design tips
- Don't duplicate: store the cart items, not also a separate "count" (compute it with
items.length). - Store ids, not copies: for a selected item, store
selectedIdand find the item from the list. - Group related values that always change together into one object.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04