Skip to content

Sharing State in React: Lifting State Up, Callback Props and Context with useContext

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

So far each component has kept its own state. Real screens need components to agree with each other: the cart badge in the header must show what the product list added, a filter in the sidebar changes the results in the middle, a language switch changes every label on the page. React solves this with one simple rule: when two components need the same data, move that state up to their closest common parent and pass it down. When many components deep in the tree need it, context lets you skip the layers in between. This lesson covers both, with the patterns you will use in every real app.

Quick answer

To share state between components, lift it up: keep it in their closest common parent, pass the value down as props, and pass a function such as onAdd down so children can ask the parent to change it. The parent is then the single source of truth. When a value is needed by many components at different depths, create a context with createContext(defaultValue), wrap part of the tree in <LangContext value={lang}>, and read it anywhere below with useContext(LangContext). Use context for widely shared values such as language, theme or the signed-in user, not for every piece of state.

40 min read and practice · Content checked on 24 November 2026

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.

Try it: A shop where the list and the cart share one state
Code
ResultUpdates as you type
Console
  1. 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
Sign in or create a free account

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.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.