In this lesson you will
- Lift state up to the closest common parent of the components that need it
- Pass data down as props and events up as callback props
- Create, provide and read a context with createContext and useContext
- Decide between props, lifted state and context
Lifting state up
Imagine a product list and a cart summary side by side. If the list keeps the cart in its own state, the summary cannot see it; if both keep a copy, the copies drift apart. The fix is to delete the state from both and keep one copy in the component that contains them, often called App or Shop. That parent passes cart down to the summary as a prop and passes a function, onAdd, down to the list. When a button in the list is clicked, it calls onAdd(product), and the parent updates its state. React then renders both children with the new cart.
This is called lifting state up, and it follows from one-way data flow: data goes down through props, requests to change it go up through callback props. Name callbacks after what happened, starting with on, such as onAdd, onRemove or onChange, and keep the real update logic in the parent next to the state. The children become simple and reusable: a ProductList that receives onAdd can be used in a shop page, a search page or a gift-box builder without knowing where the cart lives.
Console
- Console messages, errors, form data and clicked links appear here.
Only Shop has state. The header, the list and the summary all show the same cart because they all get it from one place. ProductList never touches the cart; it only calls onAdd. Add a second <CartSummary> and it stays in sync for free.
Sign in free to read the full lesson
You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.
Unlocks after sign-in:
- 3 more sections with live code examples
- Common mistakes to avoid
- Hands-on coding exercise with automatic checks
- Practice quiz and the graded lesson test
Free forever. No card, no paywall.
Questions people ask
What does lifting state up mean in React?
Moving state from child components into their closest common parent, then passing the value down as props and a change function down as a callback prop, so every child shows the same data.
Is React context a replacement for Redux?
Not exactly. Context passes a value down the tree; it does not manage updates. Combined with useState or useReducer it covers many apps. Libraries such as Redux Toolkit or Zustand add tools for large, frequently changing shared state.
Does every component re-render when a context value changes?
Every component that reads that context with useContext re-renders, plus their children as usual. Components that do not read it are not forced to. Split contexts by concern to keep updates small.
What is a custom Hook?
A function whose name starts with use and that calls other Hooks, such as useWords in this lesson. It lets components share logic, not state: each component that calls it gets its own state.
When should I use useReducer instead of useState?
When the next state depends on the previous one in several different ways, such as add, remove, toggle and clear on a list, a reducer keeps all those rules in one function. For one or two simple values, useState is clearer.
Sources
Facts in this lesson were checked against these official pages on 24 November 2026.
- React docs: Sharing State Between Components
- React docs: Passing Data Deeply with Context
- React reference: useContext
- React reference: createContext (context as a provider)
- React docs: Reusing Logic with Custom Hooks
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.