Challenge - 5 Problems
Next.js State & Hooks Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ state_output
intermediate2:00remaining
What is the output after clicking the button twice?
Consider this Next.js client component using React hooks. What will be the displayed count after clicking the button two times?
NextJS
'use client'; import React, { useState } from 'react'; export default function Counter() { const [count, setCount] = useState(0); function handleClick() { setCount(count + 1); setCount(count + 1); } return ( <button onClick={handleClick} aria-label="Increment count"> Count: {count} </button> ); }
Attempts:
2 left
💡 Hint
Remember that state updates in React are asynchronous and may be batched.
✗ Incorrect
Both setCount calls use the same stale value of count (0), so the count only increases by 1 after the two calls.
❓ component_behavior
intermediate2:00remaining
Which option correctly updates state based on previous value?
In a Next.js client component, which option correctly increments the count twice when the button is clicked?
NextJS
'use client'; import React, { useState } from 'react'; export default function Counter() { const [count, setCount] = useState(0); function handleClick() { // Which option below correctly increments count twice? } return ( <button onClick={handleClick} aria-label="Increment count"> Count: {count} </button> ); }
Attempts:
2 left
💡 Hint
Use the functional form of setState to get the latest value.
✗ Incorrect
Using the updater function prev => prev + 1 ensures each update uses the latest state value, so two increments add 2.
📝 Syntax
advanced2:00remaining
Which option causes a syntax error in a Next.js client component?
Identify the option that will cause a syntax error when used in a Next.js client component with React hooks.
NextJS
'use client'; import React, { useState } from 'react'; export default function Example() { const [value, setValue] = useState(0); return <div>{value}</div>; }
Attempts:
2 left
💡 Hint
Check the correct syntax for array destructuring assignment.
✗ Incorrect
Option C uses a comma instead of brackets for destructuring, causing a syntax error.
🔧 Debug
advanced2:00remaining
Why does this Next.js client component not update the displayed count?
This component does not update the count on button click. What is the cause?
NextJS
'use client'; import React, { useState } from 'react'; export default function Counter() { let count = 0; function handleClick() { count += 1; } return ( <button onClick={handleClick} aria-label="Increment count"> Count: {count} </button> ); }
Attempts:
2 left
💡 Hint
React only re-renders when state or props change.
✗ Incorrect
Changing a local variable does not trigger React to re-render the component, so the UI stays the same.
🧠 Conceptual
expert2:00remaining
What happens if you call a React hook conditionally inside a Next.js client component?
Consider this code snippet inside a Next.js client component. What is the result of calling useState inside a conditional block?
NextJS
'use client'; import React, { useState } from 'react'; export default function ConditionalHook({ flag }) { if (flag) { const [count, setCount] = useState(0); return <div>Count: {count}</div>; } else { return <div>No count</div>; } }
Attempts:
2 left
💡 Hint
React hooks must be called unconditionally in the same order on every render.
✗ Incorrect
Calling hooks conditionally breaks the rules of hooks, leading to unpredictable behavior or errors.