0
0
NextJSframework~10 mins

Event handlers in client components in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Event handlers in client components
User action: click/button press
Event handler function triggered
State update or side effect
Component re-renders if state changed
DOM updates to reflect new state
User sees updated UI
When a user interacts with a component, the event handler runs, updates state or triggers effects, causing the component to re-render and update the UI.
Execution Sample
NextJS
'use client';
import { useState } from 'react';

export default function ClickCounter() {
  const [count, setCount] = useState(0);
  return <button onClick={() => setCount(count + 1)}>Clicked {count} times</button>;
}
A button that counts how many times it is clicked by updating state on each click.
Execution Table
StepUser ActionState BeforeEvent Handler ActionState AfterComponent Re-renderUI Output
1Page loadscount = 0No eventcount = 0Initial renderButton shows 'Clicked 0 times'
2User clicks buttoncount = 0setCount(0 + 1)count = 1Re-renderButton shows 'Clicked 1 time'
3User clicks buttoncount = 1setCount(1 + 1)count = 2Re-renderButton shows 'Clicked 2 times'
4User clicks buttoncount = 2setCount(2 + 1)count = 3Re-renderButton shows 'Clicked 3 times'
💡 No more user clicks, so no further state changes or re-renders.
Variable Tracker
VariableStartAfter 1After 2After 3Final
count01233
Key Moments - 3 Insights
Why does the UI update after clicking the button?
Because the event handler calls setCount which updates the state, triggering React to re-render the component with the new count value (see execution_table steps 2-4).
What happens if the event handler does not update state?
The component will not re-render and the UI will not change, as React only updates the DOM when state or props change (see execution_table step 1).
Can the event handler run without user interaction?
No, event handlers run only in response to user actions like clicks or key presses (see execution_table step 1 where no event means no handler run).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'count' after the second click?
A1
B2
C3
D0
💡 Hint
Check the 'State After' column at step 3 in the execution_table.
At which step does the component first re-render?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Component Re-render' column; the first 'Re-render' (not initial) happens at step 2.
If the event handler did not call setCount, what would the UI show after clicks?
AThe button would disappear
BThe count would increase normally
CThe count would stay at 0
DAn error would occur
💡 Hint
Refer to key_moments where state update triggers UI change.
Concept Snapshot
Event handlers in client components:
- Attach functions to UI events like clicks
- Functions run on user action
- Use state setters (e.g., setCount) to update state
- State change triggers component re-render
- UI updates to reflect new state
- Always client-side for interactivity
Full Transcript
This example shows how event handlers work in Next.js client components. When the user clicks the button, the onClick handler runs. It calls setCount to increase the count state by one. React then re-renders the component with the updated count. The button text updates to show the new count. Without state change, the UI would not update. Event handlers only run on user actions like clicks. This flow ensures interactive UI updates smoothly.