Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: Routing and Application Architecture
Stage 5: Specialization (pick ONE)

Part 16 / Topic 4 of 9

Where should this state live?

examples/04_where_state_lives.jsx is one file, three stages of the same feature - "mark a restaurant as favorite" - as it grows, each stage reaching for the smallest tool that solves that stage's actual problem:

Stage Tool When it's the right call
1 - local useState inside RestaurantCardLocal Only this one component cares. Reaching for anything more before a second component needs the value solves a problem that doesn't exist yet.
2 - lifted useState in RestaurantPageLifted, passed down as props to FavoritesHeaderLifted/RestaurantCardLifted A sibling now needs to read or change the same value, and both siblings already share a direct parent - lift to that parent, no context needed yet.
3 - context + reducer-shaped updates FavoritesContext/FavoritesProvider/useFavorites A third, unrelated part of the tree (NavBarFavoritesIcon, not a descendant of the restaurant list at all) needs the same value - lifting further would mean threading it up past components that don't use it either, just to reach a sibling subtree. This is Part 9's prop-drilling pain, and context is the right answer now, not earlier, because context has a real re-render cost of its own that's wasted when lifting alone still works.
4 - the URL, via useSearchParams not in this file - Part 15 Section 5 The value needs to survive a page refresh or be shareable as a link - something neither local nor lifted useState, nor context, can ever do, since all three live only in memory and reset the instant the page reloads.

04_where_state_lives.test.jsx really proves stages 1-3: clicking a local card's button flips only that card; clicking one sibling card in the lifted version really updates the header count read by its sibling; clicking a card inside FavoritesProvider really updates NavBarFavoritesIcon, a component that is not a descendant of the card that changed at all - the exact scenario local or lifted state cannot reach. Row 4 is this section's point extended with what Part 15 already taught: examples/05_usenavigate_and_search_params.jsx in Part 15 showed landing straight on /search?cuisine=north-indian pre-filtering with no click at all - a plain useState("all") version of the same filter resets to its default on every refresh, because useState was never meant to outlive the component. The decision framework in one sentence: climb this table exactly one row at a time, and only when the row below has actually run out of room for the next reader.

Done here? The practice set for Part 16 — including the solutions — is on the part page.

Search the guide