What if you could share data across your app without endless prop passing?
Why Context API in React Native? - Purpose & Use Cases
Imagine you have a React Native app with many screens, and you want to share user settings like theme color or language across all of them.
You try passing these settings as props from one screen to another, drilling them down through many components.
Passing props through many layers is tiring and error-prone.
If you forget to pass a prop somewhere, the data won't reach the right place.
It also makes your code messy and hard to change later.
The Context API lets you share data globally without passing props manually.
You create a context provider once, and any component inside can access the shared data directly.
This keeps your code clean and easy to maintain.
function Screen({ theme }) {
return <Child theme={theme} />;
}
function Child({ theme }) {
return <Text style={{ color: theme.color }}>Hello</Text>;
}const ThemeContext = React.createContext();
function Screen() {
return <ThemeContext.Provider value={{ color: 'blue' }}>
<Child />
</ThemeContext.Provider>;
}
function Child() {
const theme = React.useContext(ThemeContext);
return <Text style={{ color: theme.color }}>Hello</Text>;
}You can easily share and update data across many components without messy prop passing.
In a shopping app, you can share the user's login status and cart contents across all screens using Context API, so every part of the app knows if the user is logged in and what items they have.
Passing data manually through props is slow and error-prone.
Context API provides a clean way to share data globally.
It makes your React Native app easier to build and maintain.
Practice
Context API?Solution
Step 1: Understand Context API's role
Context API is designed to share data globally in a React app without passing props through every level.Step 2: Compare options
Only To share data easily across many components without passing props manually describes this purpose. Others relate to styling, navigation, or data fetching, which are unrelated.Final Answer:
To share data easily across many components without passing props manually -> Option AQuick Check:
Context API = Share data easily [OK]
- Confusing Context API with styling or navigation
- Thinking it fetches data from servers
- Believing it replaces all props usage
Solution
Step 1: Recall correct syntax for creating context
React Native uses React.createContext() to create a new context object.Step 2: Analyze options
const MyContext = React.createContext(); uses the full correct syntax. const MyContext = createContext(); misses React prefix. new Context(); uses invalid constructor. React.useContext(); calls useContext which is a hook, not for creation.Final Answer:
const MyContext = React.createContext(); -> Option CQuick Check:
Create context = React.createContext() [OK]
- Omitting React prefix
- Using new Context() which doesn't exist
- Confusing useContext hook with createContext
ChildComponent?
const MyContext = React.createContext('Default');
function Parent() {
return (
);
}
function ChildComponent() {
const value = React.useContext(MyContext);
return {value};
}Solution
Step 1: Identify the Provider value
The sets the value to "Hello" for all children inside it.Step 2: Check useContext usage in ChildComponent
ChildComponent uses React.useContext(MyContext) which reads the nearest Provider's value, which is "Hello".Final Answer:
Hello -> Option DQuick Check:
useContext reads Provider value = Hello [OK]
- Thinking default value is used when Provider exists
- Assuming undefined if value is string
- Confusing error with missing Provider
const MyContext = React.createContext();
function Parent() {
return (
);
}
function Child() {
const value = React.useContext();
return {value};
}Solution
Step 1: Check useContext call in Child
useContext must be called with the context object as argument, e.g., React.useContext(MyContext).Step 2: Verify other parts
Provider has a value prop "Hi" which is correct. createContext can be called without initial value (undefined default). Child can use useContext.Final Answer:
useContext is called without passing the context object -> Option BQuick Check:
useContext needs context argument [OK]
- Calling useContext() with no arguments
- Thinking createContext must have initial value
- Believing Provider can omit value prop
React.createContext(), wrap components with Provider passing user as value, use useContext in children.
B) Pass user as props to every component manually.
C) Use useState in every component to store user.
D) Use React.createContext() but do not wrap components with Provider.Solution
Step 1: Identify correct Context API usage
To share data globally, create context, wrap components with Provider passing data, and consume with useContext.Step 2: Eliminate incorrect options
B passes props manually, not using Context API. C duplicates state unnecessarily. D creates context but no Provider means default or undefined value.Final Answer:
Create context, wrap with Provider passing user, use useContext in children -> Option AQuick Check:
Context API = create + Provider + useContext [OK]
- Skipping Provider wrapping
- Passing props manually instead of using context
- Using useState in every component unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
