Context — Sharing Without Prop Drilling — Concept Check
Questions on Context — Sharing Without Prop Drilling. Context passes a value to a whole subtree without threading props through it. It is a delivery mechanism, not a state manager, and every consumer re-renders when the value changes.
Questions in this quiz
7 questions on Context — Sharing Without Prop Drilling — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A component in your React app re-renders every time the cart updates, even though it only uses theme data. What is a likely cause?
- AThe component is not wrapped in a ThemeProvider.
- BThe component is consuming a context that includes both theme and cart data.
- CThe component is using useState instead of useContext for theme data.
- DThe component's props are being mutated directly.
2.You notice that every consumer of a context re-renders whenever any part of its value changes. How can you minimize unnecessary re-renders?
- AIncrease the number of providers in the app.
- BSplit the context by update frequency and concern.
- CAvoid using context for any state management.
- DUse more useState hooks within the context.
3.Why might using context for high-frequency state updates lead to performance issues?
- AContext requires deep prop drilling, which increases complexity.
- BContext lacks selectors, causing all consumers to re-render on any change.
- CContext automatically batches updates, which delays rendering.
- DContext does not support asynchronous updates, leading to blocking issues.
4.A developer uses context to manage a theme that changes infrequently. What is a potential downside of this approach?
- AContext is unsuitable for managing any global state, including themes.
- BThe theme cannot be accessed outside the context provider.
- CAll consumers re-render even if they don't read the changed value.
- DThe context value cannot be memoized, leading to inefficient renders.
5.What might be a better alternative to context if only one deeply nested component needs a value passed through multiple layers?
- AAvoid passing the value and access it directly in the component.
- BCreate a global state object to manage the value.
- CUse multiple nested providers to pass the value.
- DUse composition to pass the rendered element instead of the data.
6.Which of the following is NOT a good use case for context?
- ACart state that updates on every keystroke.
- BLocale settings that update infrequently.
- CTheme management that changes rarely.
- DFeature flags that change occasionally.
7.When should you consider using context in a React application?
- AWhen you want to avoid using hooks altogether.
- BWhen you have only two levels of prop drilling.
- CWhen multiple scattered components need the same value.
- DWhen you need to manage high-frequency state updates.