JavaScript Curriculum
Lifting State Up
mediumThe coffee shop menu has a SearchBar and a MenuGrid side by side. Typing in SearchBar must filter MenuGrid. They are siblings — neither can read the other's state. The solution: lift the search state to their shared parent.
Lifting State Up
When siblings need to share state, lift it to their nearest common ancestor. The parent becomes the single source of truth — it passes data down as props and callbacks down for children to trigger changes.
Before and After — The Problem and Fix
See what happens when state stays local vs when it is lifted:
// ❌ Before lifting — siblings can't share state function SearchBar() { const [search, setSearch] = useState('') // local! return <input value={search} onChange={...} /> } function MenuGrid() { // ❌ Can't access search from SearchBar! return <div>...</div> } function CartSidebar() { const [cart, setCart] = useState([]) // local! // ❌ MenuGrid can't add to this cart! }
One-Way Data Flow — Visualized
Data travels down via props, events travel up via callbacks:
Parent passes data to child via props. Child receives it as read-only. Any number of levels deep.
// App → Header → UserName <App> <Header name={name} /> {/* prop flows down */} </App> function Header({ name }) { return <h1>Hi, {name}</h1> // read-only }
Siblings in Sync — Live Demo
SearchBar, CategoryFilter, and MenuGrid — all three connected via parent state:
// ❌ Siblings can't talk to each other directly function SearchBar() { const [search, setSearch] = useState('') // Can't send search to MenuGrid — it's a sibling! } function CategoryFilter() { const [category, setCategory] = useState('all') // Can't send category to MenuGrid either! } function MenuGrid() { // ❌ Has no access to search or category // Shows all items always }
Your Challenge
Build App with search and category state. Pass search and onSearch to SearchBar. Pass category and onCategory to CategoryFilter. Pass search and category to MenuGrid which derives the visible items. All three siblings stay in sync through the parent.
Challenge
Build a filterable menu with three siblings: SearchBar (updates search), CategoryFilter (updates category), and MenuGrid (reads both). All three share state via a common parent App. Wire onSearch and onFilter callbacks upward.
Lifting State Up
mediumThe coffee shop menu has a SearchBar and a MenuGrid side by side. Typing in SearchBar must filter MenuGrid. They are siblings — neither can read the other's state. The solution: lift the search state to their shared parent.
Lifting State Up
When siblings need to share state, lift it to their nearest common ancestor. The parent becomes the single source of truth — it passes data down as props and callbacks down for children to trigger changes.
Before and After — The Problem and Fix
See what happens when state stays local vs when it is lifted:
// ❌ Before lifting — siblings can't share state function SearchBar() { const [search, setSearch] = useState('') // local! return <input value={search} onChange={...} /> } function MenuGrid() { // ❌ Can't access search from SearchBar! return <div>...</div> } function CartSidebar() { const [cart, setCart] = useState([]) // local! // ❌ MenuGrid can't add to this cart! }
One-Way Data Flow — Visualized
Data travels down via props, events travel up via callbacks:
Parent passes data to child via props. Child receives it as read-only. Any number of levels deep.
// App → Header → UserName <App> <Header name={name} /> {/* prop flows down */} </App> function Header({ name }) { return <h1>Hi, {name}</h1> // read-only }
Siblings in Sync — Live Demo
SearchBar, CategoryFilter, and MenuGrid — all three connected via parent state:
// ❌ Siblings can't talk to each other directly function SearchBar() { const [search, setSearch] = useState('') // Can't send search to MenuGrid — it's a sibling! } function CategoryFilter() { const [category, setCategory] = useState('all') // Can't send category to MenuGrid either! } function MenuGrid() { // ❌ Has no access to search or category // Shows all items always }
Your Challenge
Build App with search and category state. Pass search and onSearch to SearchBar. Pass category and onCategory to CategoryFilter. Pass search and category to MenuGrid which derives the visible items. All three siblings stay in sync through the parent.
Challenge
Build a filterable menu with three siblings: SearchBar (updates search), CategoryFilter (updates category), and MenuGrid (reads both). All three share state via a common parent App. Wire onSearch and onFilter callbacks upward.