Sibling Components Don't Share State
You'll learn: Why two separate instances of the same component can't access each other's state—and what that means for your app architecture.
The Core Principle
When you render the same component multiple times, each instance gets completely independent state. They don't share anything. Think of them like two separate apartments in the same building—they have the same floor plan (component code), but different furniture (state).
function Counter() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>
Clicked {count} times
</button>
);
}
function App() {
return (
<>
<Counter /> {/* Has its own count */}
<Counter /> {/* Has its own separate count */}
</>
);
}
Clicking the first button increments only the first counter. Clicking the second button increments only the second counter. They're siblings in the component tree, but complete strangers to each other's state.
When You Need Shared State
If two components need to display or update the same value, you can't declare the state in both places. You must lift the state up to their common parent, then pass it down as props:
function App() {
const [sharedCount, setSharedCount] = useState(0);
return (
<>
<Counter count={sharedCount} setCount={setSharedCount} />
<Counter count={sharedCount} setCount={setSharedCount} />
</>
);
}
Now both counters show and update the same state, because it lives in their parent.
Key Takeaway: Sibling components never share state automatically—if they need to coordinate, lift the shared state to a common parent and pass it down as props.