Q1. What is the Context API in React?
The Context API is a feature that allows you to share data across the component tree without passing props manually at every level (prop drilling). It consists of React.createContext, Provider, and Consumer (or useContext hook).
Q2. When should you use Context API?
Use it for global data like theme, user authentication, language preferences, or UI state that many components need. For simpler cases, prop drilling may suffice. Avoid overusing context for every piece of state.
Q3. How does Context API work?
First, create a context with React.createContext(). Then wrap a parent component with . Any child component can consume the context using or the useContext(MyContext) hook.
Q4. What is the difference between Context API and props?
Props pass data from parent to child only. Context allows data to be accessed by any component in the tree, regardless of depth, without intermediate components having to pass it down.
Q5. Can you update context from a consumer?
Yes, you can pass functions down through context that allow consumers to update the context value. Typically, you define state in the provider and pass the state and setter down together.
