Consuming Context with useContext
Read context values inside any nested component using the useContext hook, avoiding the need to pass props through intermediate components.
Reading Context with useContext
The useContext(ContextObject) hook lets any function component subscribe to a context and read its current value. It replaces the older Consumer render prop pattern with a simple hook call. The component automatically re-renders whenever the context value changes.
import { useContext } from 'react';
import { ThemeContext } from '../context/ThemeContext';
function Header() {
const { theme } = useContext(ThemeContext);
return (
<View style={{ backgroundColor: theme === 'dark' ? '#1a1a2e' : '#fff' }}>
<Text>My App</Text>
</View>
);
}No Prop Drilling Required
The main benefit of useContext is that the consuming component does not need to receive props from its parent. The Header component in the previous example can be anywhere in the tree — three levels deep, inside a modal, or in a completely different branch — and it still reads the same theme value directly from context.
// Without context: theme prop drills through App→Screen→Section→Card→Header
// With context: Header reads theme directly from context
function DeepComponent() {
const { user } = useContext(AuthContext); // no props needed
return <Text>Welcome, {user.name}</Text>;
}All lessons in this course
- Creating and Providing a Context
- Consuming Context with useContext
- A Theme Context with Dark Mode Toggle
- Avoiding Context Performance Issues