Lift state up

React chapter 141 · Props down, events up · one source of truth · source: react.dev/learn/sharing-state-between-components

The problem

An FAQ accordion should open one answer at a time. Each panel keeps its own isOpen, so panels cannot see each other: you can open all three, and the header cannot tell which one is open.

The pattern that fixes it

Move the state to the closest common parent. The accordion owns activeIndex, passes isActive down, and passes onShow down so a panel can ask for a change. Data flows down, requests flow up, and two-open becomes impossible.

In plain words

Two components that must agree need one shared owner. The parent keeps the value and hands each child a copy, plus a function the child can call to ask for changes.

A single TV remote held by the parent. Kids can ask “switch to channel 2”, but nobody has their own remote fighting over the same TV.

Terms

props down
The parent passes values to children. Children read them; they do not change them.
events up
The parent also passes callbacks (onShow). A child calls one to ask the parent to change its state.
single source of truth
Each piece of state lives in exactly one component. Everyone else derives from it.

Going deeper

Controlled and uncontrolled, again

A panel with its own isOpen is uncontrolled; a panel driven by isActive is controlled. Libraries ship both by letting the parent pass a value or not (chapter 2, useControlled). Lifting state is choosing the controlled shape at the app level.

Lift to the lowest common parent, not the top

Lifting too high makes every keystroke re-render half the app. Put state as low as possible and as high as necessary; move it back down when it is only used in one place (chapter 89), or pass slow parts as children (chapter 90).

When lifting turns into prop drilling

If the common parent is many levels away, pass the value through context, or a store with selectors when updates are frequent (chapters 50, 52, 94). The ownership rule does not change, only the delivery.

Keep learning

Overview

Free returns within 30 days. Ships in 2 working days. Cards, UPI and PayPal. Real life: accordions, tabs, a filter box and the list it filters, a cart badge and the cart. no state — it cannot know which panel is open

Three owners, no agreement.

Each panel is right about itself and wrong about the rule. Siblings cannot read each other’s state. Open Returns, then Shipping. Both stay open. Notice only the clicked Panel re-renders: the Accordion is never involved, so it can never enforce the rule. State in the parent, props down, events up props ↓ mono tg halo onShow(1) ↑ state in the lowest common parent · props down · events up owns state Same FAQ. The accordion owns the one value everyone must agree on. Props down, events up Accordion runs setActiveIndex(1). Its state changed, so it re-renders. New props flow down: Returns gets false, Shipping gets true. Two open panels are now impossible. Rule: put state in the closest common parent of everyone who needs it — and no higher. down: value · up: callback

Exactly one panel open, and the parent always knows which.

Watch the badges: a click re-renders the Accordion and its panels together, because the state now lives above them.

More courses

  • Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
  • Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
  • Databases in depth — How a query becomes work. Indexes, isolation, locks, and the plans that explain why a page is slow — not an ORM tour.
  • System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
  • NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
  • Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
  • Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
  • AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
  • Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
  • TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
  • CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
  • Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
  • Docker — What an image is, why layers cache, and how containers find each other on a network.
  • Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
  • Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
  • AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
  • Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.