Challenge - 5 Problems
Hook Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this custom hook usage?
Consider this React component using a custom hook that counts clicks. What will be displayed after clicking the button 3 times?
React
import React, { useState } from 'react'; function useClickCounter() { const [count, setCount] = useState(0); function increment() { setCount(c => c + 1); } return { count, increment }; } export default function Clicker() { const { count, increment } = useClickCounter(); return ( <> <button onClick={increment}>Click me</button> <p>Clicked {count} times</p> </> ); }
Attempts:
2 left
💡 Hint
Think about how the state updates with each click and how the count is displayed.
✗ Incorrect
The custom hook useClickCounter manages a count state starting at 0. Each click calls increment, which increases count by 1. After 3 clicks, count is 3, so the paragraph shows 'Clicked 3 times'.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a custom hook that returns a boolean toggle?
You want a custom hook named useToggle that returns a boolean state and a function to flip it. Which code is correct?
Attempts:
2 left
💡 Hint
Remember to use the functional update form to avoid stale state.
✗ Incorrect
Option A correctly uses the functional update form setValue(value => !value) to toggle the boolean state and returns an object with value and toggle function. This avoids stale closure issues.
🔧 Debug
advanced2:00remaining
Why does this custom hook cause an infinite loop?
Examine this custom hook and component. Why does it cause an infinite render loop?
function useData() {
const [data, setData] = useState(null);
fetch('https://api.example.com/data')
.then(res => res.json())
.then(setData);
return data;
}
function Component() {
const data = useData();
return
{data ? data.message : 'Loading...'}
;
}Attempts:
2 left
💡 Hint
Think about what happens when state updates inside a hook without controlling side effects.
✗ Incorrect
The fetch call runs on every render because it is not inside useEffect. Each fetch updates state with setData, triggering a re-render, which triggers fetch again, causing an infinite loop.
❓ state_output
advanced2:00remaining
What is the final count displayed after clicking the button twice?
Given this component using a custom hook, what number will show after clicking the button two times?
function useCounter() {
const [count, setCount] = useState(0);
function increment() {
setCount(count + 1);
setCount(count + 1);
}
return { count, increment };
}
function Counter() {
const { count, increment } = useCounter();
return ;
}
Attempts:
2 left
💡 Hint
Remember how React batches state updates and how stale state values affect setState calls.
✗ Incorrect
Each button click calls increment(), which executes two setCount(count + 1) using the stale count value captured in the closure from that render. React batches the identical updates into a single state update, so the count increases by only 1 per click. After two clicks, the count is 2, displaying 'Count: 2'.
🧠 Conceptual
expert2:00remaining
Which statement best explains why custom hooks must start with 'use'?
Why do React custom hooks have to start with the word 'use'?
Attempts:
2 left
💡 Hint
Think about how React detects hooks to apply special rules.
✗ Incorrect
React uses the 'use' prefix to detect hooks and apply the rules of hooks, like calling hooks only at the top level and in React functions. This detection happens during compilation and runtime.