------------------------- | Global State Demo | |-----------------------| | User Name: [Alice] | | | | [Change Name Button] | -------------------------
Why global state avoids prop drilling in React Native - Build It to Prove It
import React, { createContext, useState } from 'react'; import { View, Text, Button } from 'react-native'; const UserContext = createContext(); function DeepChild() { // TODO: Access user name from global state here return ( <View> <Text>User Name: {/* Show user name here */}</Text> </View> ); } function MiddleChild() { return <DeepChild />; } export default function App() { const [userName, setUserName] = useState('Alice'); // TODO: Provide userName and setUserName via UserContext.Provider return ( <View style={{ padding: 20 }}> <Text style={{ fontSize: 20, marginBottom: 10 }}>Global State Demo</Text> <MiddleChild /> {/* TODO: Add button to change user name here */} </View> ); }
import React, { createContext, useState, useContext } from 'react'; import { View, Text, Button } from 'react-native'; const UserContext = createContext(); function DeepChild() { const { userName } = useContext(UserContext); return ( <View> <Text>User Name: {userName}</Text> </View> ); } function MiddleChild() { return <DeepChild />; } export default function App() { const [userName, setUserName] = useState('Alice'); return ( <UserContext.Provider value={{ userName, setUserName }}> <View style={{ padding: 20 }}> <Text style={{ fontSize: 20, marginBottom: 10 }}>Global State Demo</Text> <MiddleChild /> <Button title="Change Name" onPress={() => setUserName(userName === 'Alice' ? 'Bob' : 'Alice')} accessibilityLabel="Change user name button" /> </View> </UserContext.Provider> ); }
We use React's Context API to create a global state called UserContext. The App component holds the userName state and provides it to all child components via UserContext.Provider. The DeepChild component accesses the userName directly from the context using useContext, so it does not need to receive it as a prop from MiddleChild or App. This avoids prop drilling, which means passing props through many layers just to reach a deeply nested component. The button in App changes the global userName, and the change is reflected immediately in DeepChild because it reads from the shared global state.
This approach keeps the code clean and easy to maintain, especially when many components need access to the same data.
------------------------- | Global State Demo | |-----------------------| | User Name: Alice | | | | [Change Name Button] | -------------------------
Practice
Solution
Step 1: Understand prop drilling
Prop drilling means passing data through many layers of components even if intermediate ones don't need it.Step 2: Understand global state benefits
Global state shares data directly with any component that needs it, avoiding unnecessary prop passing.Final Answer:
Because global state allows direct data sharing without passing props through many components -> Option DQuick Check:
Global state avoids prop drilling = C [OK]
- Thinking prop drilling improves performance
- Believing global state requires more code
- Confusing prop drilling with avoiding state
Solution
Step 1: Recall React Context usage
To access global state via React Context, useContext hook is used with the context object.Step 2: Identify correct hook
useState manages local state, useEffect manages side effects, useReducer manages complex state logic, but none access context directly.Final Answer:
const value = useContext(MyContext); -> Option CQuick Check:
Access context with useContext = D [OK]
- Using useState instead of useContext for context
- Confusing useEffect with state access
- Trying to use useReducer directly for context
const CountContext = React.createContext(0);
function Display() {
const count = React.useContext(CountContext);
return {count};
}
function App() {
const [count, setCount] = React.useState(0);
return (
setCount(count + 1)} />
);
}Solution
Step 1: Understand context and state connection
The count state is provided via CountContext.Provider and updated on button press.Step 2: Trace button press effect
Pressing the button calls setCount(count + 1), updating count and re-rendering Display with new value.Final Answer:
The displayed number increases by 1 each button press -> Option BQuick Check:
Context updates with state changes = B [OK]
- Assuming context value never changes
- Thinking button does not update state
- Believing app crashes without provider
const UserContext = React.createContext();
function Profile({ user }) {
return {user.name};
}
function App() {
const user = { name: 'Anna' };
return (
);
}Solution
Step 1: Check how Profile receives data
Profile expects a user prop but is rendered without props, causing undefined user.Step 2: Use context to avoid prop drilling
Profile should use useContext(UserContext) to get user directly from global state.Final Answer:
Profile component should use useContext(UserContext) instead of props -> Option AQuick Check:
Use context hook to avoid prop drilling = A [OK]
- Forgetting to use useContext in child components
- Assuming Provider needs no value prop
- Thinking class components are required
Solution
Step 1: Understand prop drilling problem
Passing props through many nested components is tedious and error-prone.Step 2: Use global state with React Context
React Context allows sharing user data globally, so any component can access it directly without prop passing.Final Answer:
Use React Context to create a global user state and access it with useContext in any component -> Option AQuick Check:
Global state with context avoids prop drilling = A [OK]
- Passing props through every nested component
- Using local state only at top level
- Duplicating state in nested components
Ready to prove it?
Pass the test and this pattern moves unknown
