What is the Context API pattern for global state in React Native?
Why Interviewers Ask This
This tests whether you can apply React Native knowledge to real-world scenarios. Interviewers are looking for clarity of thought and evidence that you've encountered this in production code.
Answer
The Context API is React's built-in solution for sharing state across the component tree without prop drilling. It's commonly used in React Native for: app theme, user authentication, language/locale, cart state. Complete auth context example: // contexts/AuthContext.tsx: interface AuthContextType { user: User | null; login: (creds: Credentials) => Promise<void>; logout: () => Promise<void>; isLoading: boolean; } const AuthContext = createContext<AuthContextType>(null!); export function AuthProvider({ children }: { children: ReactNode }) { const [user, setUser] = useState<User | null>(null); const [isLoading, setIsLoading] = useState(true); const login = async (creds: Credentials) => { const userData = await api.login(creds); await SecureStore.setItemAsync("token", userData.token); setUser(userData.user); }; const logout = async () => { await SecureStore.deleteItemAsync("token"); setUser(null); }; return ( <AuthContext.Provider value={{ user, login, logout, isLoading }}> {children} </AuthContext.Provider> ); } export const useAuth = () => useContext(AuthContext); // Usage in any component: function ProfileScreen() { const { user, logout } = useAuth(); return ( <View> <Text>{user?.name}</Text> <Button title="Logout" onPress={logout} /> </View> ); }. Performance: every context consumer re-renders when context value changes. Split contexts by update frequency — separate frequently-updating state (user location) from rarely-updating (theme). Or use context + useReducer for complex state with memoization.
Pro Tip
Back up your answer with a specific project or situation. Saying 'In my last React Native project, I used this when...' immediately makes your answer more credible and memorable.
Previous
What is react-native-reanimated Shared Values and Worklets?
Next
How does the React Native New Architecture improve performance?
More React Native Questions
View all →- Intermediate What is the React Native New Architecture?
- Intermediate What is React Navigation nested navigators?
- Intermediate How do you handle network requests in React Native?
- Intermediate What are Native Modules in React Native?
- Intermediate What is the difference between react-native-reanimated and the Animated API?