Custom Hooks in React
15 questions · 12 min · Medium
Test your knowledge of the React Context API, including createContext, Provider, useContext, consuming context, default values, and sharing data across components.
No sign-up needed · Last updated:
Score: 0 00:00
Create a free account to keep your scores and track your progress. It is optional.
15 questions · 12 min · Medium
20 questions · 15 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
A. To share data across components without passing props through every intermediate component
Context provides a way to make values available to components deeper in the tree without manually passing props through each level.
A. createContext
React's createContext API creates a context object that can provide a value to descendant components.
A. useContext
The useContext hook reads and subscribes to a context value from the nearest matching provider.
A. Using the context provider
A provider makes a context value available to components below it in the component tree.
A. value
The traditional Context Provider API uses the value prop to specify the context value supplied to descendants.
A. The current value of MyContext from the nearest matching provider
useContext reads the value from the nearest corresponding provider above the component in the tree.
A. The context's default value is used
If no matching provider exists above the component, useContext returns the default value supplied when the context was created.
A. const ThemeContext = createContext("light");
createContext accepts a default value and returns a context object.
A. Theme, locale, or authenticated user information
Context is commonly useful for cross-cutting data such as themes, localization settings, and authenticated user information.
A. Yes
A context value can be any JavaScript value, including objects containing state, functions, or other data.
A. Consumers using that context can re-render
When a provider's context value changes, components that consume that context can be updated and re-rendered.
A. Context can help avoid prop drilling for shared values
Context provides a way to share values with deeply nested components without explicitly passing those values through every intermediate component.
A. Provide state and an update function through the context value
A provider can combine state with functions such as setters or dispatch functions and expose them through the context value.
A. Changes to a context value can cause consuming components to re-render
When a context value changes, components that consume that context may re-render, so context values should be structured carefully when performance matters.
A. It provides a mechanism for passing shared values through a component tree without manually passing props at every level
The Context API is designed for sharing values across a component tree, particularly when passing props through many intermediate components would be inconvenient.