useContext Cannot Read from Sibling Providers
What you'll learn: Why useContext only sees Providers that are ancestors in the component tree, not siblings or children.
The Ancestor-Only Rule
When a component calls useContext, React walks up the component tree to find the nearest matching Provider. It only looks at parent, grandparent, and higher ancestors—never at siblings, cousins, or descendants.
This means if two components are side-by-side in the tree, one component's Provider is completely invisible to the other component, even if they're both rendered at the same level.
Why This Matters
<App>
<UserProvider value={user}>
<UserProfile /> {/* ✅ Can access UserProvider */}
</UserProvider>
<ThemeProvider value={theme}>
<Button /> {/* ❌ Cannot access UserProvider */}
</ThemeProvider>
</App>
In this example:
UserProfilecan calluseContext(UserContext)successfully becauseUserProvideris its ancestorButtoncannot access the user data—UserProvideris its sibling, not an ancestor- Similarly,
UserProfilecannot accessThemeProvider's value
The Mental Model
Think of Context like asking your family tree for information. You can ask your parents, grandparents, and great-grandparents. You cannot ask your siblings, cousins, or your own children—they're not in your direct ancestral line.
The Solution
If multiple branches need the same context, move the Provider above all of them:
<UserProvider value={user}>
<UserProfile /> {/* ✅ Can access */}
<Button /> {/* ✅ Can access */}
</UserProvider>
Key Takeaway: useContext only reads from Providers higher in the tree—sibling or child Providers are invisible because React searches upward, never sideways or down.