0
0
NextJSframework~20 mins

Event handlers in client components in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Event Handler Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when the button is clicked?
Consider this Next.js client component. What will be the displayed output after clicking the button once?
NextJS
'use client';

import React, { useState } from 'react';

export default function ClickCounter() {
  const [count, setCount] = useState(0);

  function handleClick() {
    setCount(count + 1);
  }

  return (
    <button onClick={handleClick} aria-label="Increment count">
      Count: {count}
    </button>
  );
}
AThe button text changes to 'Count: 1' after one click.
BThe button causes a runtime error when clicked.
CThe button text changes to 'Count: 0' after one click.
DThe button text remains 'Count: 0' even after clicking.
Attempts:
2 left
💡 Hint
Remember how useState updates state and triggers re-render.
📝 Syntax
intermediate
2:00remaining
Which option correctly attaches an event handler in a Next.js client component?
Select the option that correctly defines a button with an onClick event handler that calls the function doSomething.
NextJS
function doSomething() {
  console.log('Clicked');
}

export default function MyButton() {
  return (
    <button /* event handler here */>Click me</button>
  );
}
A<button onClick={doSomething()}>Click me</button>
B<button onclick="doSomething()">Click me</button>
C<button onClick="doSomething()">Click me</button>
D<button onClick={doSomething}>Click me</button>
Attempts:
2 left
💡 Hint
In React and Next.js, event handlers are passed as functions, not strings or function calls.
🔧 Debug
advanced
2:30remaining
Why does this event handler not update the count?
This Next.js client component does not update the count when the button is clicked. What is the cause?
NextJS
'use client';

import { useState } from 'react';

export default function Counter() {
  const [count, setCount] = useState(0);

  function handleClick() {
    count = count + 1;
  }

  return (
    <button onClick={handleClick} aria-label="Increment">
      Count: {count}
    </button>
  );
}
AThe component is missing 'use client' directive.
BThe event handler is not attached correctly to the button.
CDirectly modifying the state variable 'count' does not trigger a re-render; use setCount instead.
DThe initial state value should be a string, not a number.
Attempts:
2 left
💡 Hint
In React, state variables are immutable and must be updated via their setter functions.
state_output
advanced
2:30remaining
What is the output after clicking the button twice quickly?
Given this Next.js client component, what will be the displayed count after clicking the button twice quickly?
NextJS
'use client';

import { 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}
    </button>
  );
}
ACount: 1
BCount: 2
CCount: 0
DCount: 3
Attempts:
2 left
💡 Hint
React batches state updates and uses the current state value when calling setState with a value.
🧠 Conceptual
expert
3:00remaining
Why must Next.js client components use 'use client' for event handlers?
Select the best explanation why event handlers in Next.js require the component to be a client component with the 'use client' directive.
ABecause 'use client' automatically optimizes event handlers for server-side rendering.
BBecause event handlers require JavaScript to run in the browser, and 'use client' enables client-side rendering and interactivity.
CBecause Next.js disables all event handlers by default unless 'use client' is present.
DBecause 'use client' allows event handlers to run on the server during initial render.
Attempts:
2 left
💡 Hint
Think about where event handlers run and what 'use client' enables.