Wrap the handler in an arrow function inside the JSX, and call your handler with the values you need. React calls the arrow on click, and the arrow calls your handler with the item's ID.
There are two common ways to do it:
An arrow function, like onClick={() => handleClick(item.id)}, puts the call and its arguments right in the JSX.
The function's bind method pre-fills the arguments, as in handleClick.bind(null, item.id). React passes the event after them.
To use the event as well, pass it along with (e) => handleClick(item.id, e). Both ways create a new function on every render. That only matters when the handler goes to a memoized child.
Rewriting in plainer words…
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Saved in this browser - sign in to keep your review list.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Keep going - a few more words and AI can grade it.
Interview lens
How interviewers actually use this question
Likely follow-ups
What happens if you write onClick={handleClick(item.id)} instead?It runs during render and hands React the return value. If the handler sets state, you get a re-render loop.
A memoized row re-renders on every parent render. How would you fix the handler you pass it?Pass a stable function from useCallback and let the row call it with its own ID, so React.memo's prop comparison holds.
How could you get the item's ID in the handler without creating a closure per item?Put the ID in a data-* attribute and read e.currentTarget.dataset in one shared handler. Dataset values come back as strings.
You wrap the call in an arrow function in the JSX. React calls the arrow on click, and the arrow calls your handler with the values you need.
In a list that looks like onClick={() => handleClick(item.id)}, so each button's arrow holds on to its own item's ID.
If you also need the event, take it in the arrow and pass it along, as in (e) => handleClick(item.id, e).
The other way is bind. handleClick.bind(null, item.id) pre-fills the ID, and React passes the event after it.
Both ways create a new function on every render. I wouldn't worry about that unless the handler goes to a memoized child, because that's the only place it matters.
Weak answers to avoid
Calls the handler directly in the JSXonClick={handleClick(item.id)} runs on render and passes the return value to React. If it sets state, it loops. Wrap the call in an arrow instead.
Treats a new function per render as a bugWrapping every handler in useCallback adds noise and gains nothing. Say the cost only shows up when the handler goes to a memoized child.
Gets the argument order wrong with bindWith handleClick.bind(null, item.id), React passes the event after the bound ID. A handler written as (e, id) reads the wrong values.
Interview lens
Likely follow-ups, what you can say, and the weak answers to avoid.