Sharing State and Lifting State Up in React
Learn when sibling React components need shared state, how to lift it to their nearest common owner, and why local state often beats global state.
Lifting state gives sibling components one shared value by moving ownership to their nearest common parent. The parent passes that value down and uses callbacks to receive updates, which keeps a picker and preview in sync. This guide compares local, lifted, and global state, and explains when context or a store earns its added scope.
Sharing State and Lifting State Up in React
Introduction
Two React components may need the same value: a picker changes a color, and a preview displays it. Separate copies can disagree. Move the state to their nearest shared parent and pass the value and an update callback down. This is called lifting state up.
Keep state local when only one component needs it. Lift it when multiple components must stay in sync. The nearest shared owner is usually enough; a global store is rarely the first step.
This guide follows that value and callback through a small component tree and identifies when a shared store may be warranted.
Put shared state in a common owner
The parent owns the color because both children need it. It passes the current value down and gives the picker a callback for changes:
import { useState } from "react";
type Color = "cyan" | "magenta";
interface ColorPickerProps {
color: Color;
onSelect: (color: Color) => void;
}
function ColorPicker({ color, onSelect }: ColorPickerProps) {
return (
<div>
<button
type="button"
aria-pressed={color === "cyan"}
onClick={() => onSelect("cyan")}
>
Cyan {color === "cyan" ? "(selected)" : ""}
</button>
<button
type="button"
aria-pressed={color === "magenta"}
onClick={() => onSelect("magenta")}
>
Magenta {color === "magenta" ? "(selected)" : ""}
</button>
</div>
);
}
function ColorPreview({ color }: { color: Color }) {
return <p>Current color: {color}</p>;
}
function ColorTool() {
const [color, setColor] = useState<Color>("cyan");
return (
<>
<ColorPicker color={color} onSelect={setColor} />
<ColorPreview color={color} />
</>
);
}
Follow the state flow
flowchart TD
A[ColorTool owns color] --> B[Pass selected value]
A --> C[Pass onSelect callback]
B --> D[ColorPicker]
C --> D
D -->|User chooses color| E[Callback updates parent]
E --> A
A --> F[ColorPreview receives new value]
This works for sibling controls, filters and results, or a form field and summary. Lift only as high as needed; a common page component can pass the value to related sections.
Trade-Off Table
| Approach | Use it when | Trade-off |
|---|---|---|
| Local state | One component owns and uses the value | Another component cannot read it directly |
| Lift to a parent | A few related children need one source of truth | Parent passes more props to its children |
| Global state | Many distant areas need shared state across routes | Adds setup and a broader update surface |
Avoid moving every shared value to a global store. A parent and explicit props are easier to follow. Consider context or a state library when distant components need the value or it must persist across routes.
Production Failure Scenarios
If the picker and preview each store a color, a click may update only the button and leave the preview stale. Remove duplicate state and let their nearest common parent pass one value to both. A second failure occurs when state is lifted to the app root for two nearby controls, creating long prop chains and unrelated rerenders. Move ownership back to their nearest common parent.
Security and Compliance Notes
Props are not an access-control mechanism. A selected value can guide the interface, but it cannot authorize a sensitive action. Validate permissions and submitted data on the server, and do not put secrets in client state.
Observability Checklist
- Record the action and request ID if a state change triggers a network operation.
- Track failures at that boundary.
- Keep sensitive values out of logs; pure UI updates need no telemetry.
Common Pitfalls / Anti-Patterns
Avoid keeping duplicate copies of one value, lifting state farther than its consumers, or treating visible state as permission. Pass the current value down and a focused callback up.
When to Use / When NOT to Use
Lift state when sibling components must read or update the same changing value. Keep it local when one component owns it. Do not add a global store for a small, nearby group; consider context or a store when many distant areas truly share the value.
Quick Recap Checklist
- Keep a value local if one component owns it.
- Lift state to the nearest shared parent when siblings need the same value.
- Pass current values down and callbacks up.
- Avoid duplicate copies and premature global state.
- Treat UI state as display data, not authorization.
Interview Questions
Lift it when two or more components need the same changing value. Put it in their nearest common owner so they read one source of truth.
The parent passes a callback prop. The child calls it with the new value, and the parent updates its state.
Consider global state when many distant components need shared values or state must span routes. For a few related components, a common parent is simpler.
It is the closest component in the tree that contains all components using the shared value.
- Store the value there so siblings receive one source of truth.
- It limits how far state and callbacks need to travel.
The owner passes values down and callbacks down for children to report changes upward.
- The owner applies the update.
- Other children render from the updated shared value.
No. Lift only to a parent shared by the components that need the value.
- Local state keeps unrelated components independent.
- Excessive lifting creates more prop passing and rerenders.
They may each keep a separate copy and only one copy receives an update.
- Remove duplicate state when both should show the same value.
- Pass the shared value from their common owner.
No. UI state controls what the interface displays, not whether a request is permitted.
- Enforce permissions on the server.
- Do not put secrets in client state.
Context can help when many distant descendants need the same value.
- Use parent props for a small, related component group.
- Choose context when it makes ownership and updates easier to follow.
Send the smallest value the owner needs to perform the update.
- A selected
Coloris enough for the picker example. - Do not expose or duplicate the parent's entire state object.
Further Reading
- For the underlying setter behavior, read React state with useState.
- Props, children, and composition shows how parents pass values and callbacks through component APIs.
- Compare deeper subtree sharing in context for shared state.
- See controlled inputs and forms for shared ownership between a field and its surrounding UI.
- If local and lifted state no longer fit, read advanced React state management.
- React’s guides cover sharing state between components, passing data with context, and state as a component’s memory.
Conclusion
When siblings must stay in sync, give their nearest common parent ownership of the shared value. Pass that value down and callbacks back up. Start with local state and lift it only when the component relationships call for sharing.
Category
Related Posts
Context for Shared State in React
Learn how React context providers pass typed values to consumers, when context helps avoid prop drilling, and when local state or a store fits better.
Controlled Inputs and Forms in React
Learn to control form values with React state, type change and submit events, show validation feedback, and handle form data safely with TypeScript.
React Event Handling and Event Types
Learn to type React click, change, and form handlers in TypeScript, pass callbacks through props, and validate user input safely with clear examples.