What is the React context API?

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).