What is the React context API?

Why Interviewers Ask This

Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid React.js basics — a prerequisite for any developer role.

Answer

The Context API provides a way to share values across the component tree without explicitly passing props through every level. It is designed for "global" data that many components need: current authenticated user, theme, language preference. Creating context: const UserContext = React.createContext(defaultValue);. The default value is used when a component consumes the context but is not inside a Provider. Providing context: <UserContext.Provider value={{ user, logout }}><App /></UserContext.Provider>. Consuming context: const { user, logout } = useContext(UserContext);. Any component inside the Provider that calls useContext(UserContext) re-renders when the context value changes. Context is NOT a state manager — it is a dependency injection mechanism. It does not replace Redux for complex state; it eliminates prop drilling. Context is best for infrequently changing values. For frequently changing values, use it with a reducer or an optimized state library. Multiple contexts can be nested for different concerns (theme, user, locale).

Pro Tip

Before answering, structure your response: one-line definition → real-world analogy → concrete example from a project. This makes even complex React.js answers easy to follow.