Custom Hooks — Concept Check
Questions on Custom Hooks. A custom hook extracts stateful logic so it can be reused and tested. It shares behaviour, never state — every caller gets its own copy.
Questions in this quiz
8 questions on Custom Hooks — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.You have two components, ComponentA and ComponentB, both using the useCounter hook. What will happen to the counters in these components?
- ABoth components will share the same counter state.
- BEach component will have its own separate counter.
- CThe counters will only work if wrapped in a context provider.
- DThe counter state will be reset each time a component re-renders.
2.A developer extracts a custom hook to handle a common state and effect pattern in two components. What is a potential benefit of this extraction?
- AIt automatically optimizes the performance of the components.
- BIt reduces the number of re-renders in the components.
- CIt allows the components to have independent state logic.
- DIt makes the components share the same state.
3.In a component, a developer calls a hook conditionally inside an if statement. What problem might this cause?
- AIt will cause the hook to execute twice.
- BThe hook will not execute at all.
- CIt will cause the component to re-render infinitely.
- DIt may lead to incorrect state management due to call order mismatch.
4.A developer wants to store a value in local storage whenever it changes. Which custom hook pattern is suitable for this task?
- AuseDebouncedValue
- BuseCounter
- CuseEffectOnly
- DuseLocalStorage
5.You notice a component's logic is long and hides the markup. What is a recommended approach to improve this component?
- AExtract the logic into a custom hook.
- BUse more inline styles to simplify.
- CCombine the logic with another component.
- DRemove some of the logic to reduce complexity.
6.A developer creates a custom hook that only wraps a useState call. What is a potential downside of this approach?
- AIt improves code readability significantly.
- BIt automatically optimizes state updates.
- CIt adds unnecessary indirection without much benefit.
- DIt ensures state sharing across components.
7.A component uses a custom hook that wraps a media query API. What is a likely reason for extracting this logic into a hook?
- ATo share the media query state across all components.
- BTo encapsulate and reuse the logic for handling media queries.
- CTo ensure the media query runs only once.
- DTo automatically update the UI without re-renders.
8.What is a rule to follow when calling hooks in a React component?
- ACall hooks after rendering the component.
- BCall hooks only from the top level of the component.
- CCall hooks inside loops for dynamic state management.
- DCall hooks conditionally to optimize performance.