LearnThatStack Ace your next interview
React · question
Question 15 of 103

How do you pass parameters to event handlers?

beginner
← All React questions
Re-explain

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:

  1. An arrow function, like onClick={() => handleClick(item.id)}, puts the call and its arguments right in the JSX.
  2. The function's bind method pre-fills the arguments, as in handleClick.bind(null, item.id). React passes the event after them.
function ItemList({ items }) {
  const handleClick = (id) => {
    console.log('Clicked item:', id);
  };
  
  return (
    <div>
      {items.map(item => (
        <button key={item.id} onClick={() => handleClick(item.id)}>
          {item.name}
        </button>
      ))}
    </div>
  );
}

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.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for React One question at a time. Answer out loud, get graded, see what you missed.
How well did you know this?
AI:

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.
  • How would a child component send the clicked item's ID back up to its parent? The parent passes a callback prop, and the child calls it with the ID, for example onSelect(item.id).
  • What is the event object your handler receives? Is it the browser's native event? It is React's SyntheticEvent wrapper with the standard event interface. e.nativeEvent gives you the underlying browser event.

What you can say

  1. 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.
  2. In a list that looks like onClick={() => handleClick(item.id)}, so each button's arrow holds on to its own item's ID.
  3. If you also need the event, take it in the arrow and pass it along, as in (e) => handleClick(item.id, e).
  4. The other way is bind. handleClick.bind(null, item.id) pre-fills the ID, and React passes the event after it.
  5. 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 JSX onClick={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 bug Wrapping 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 bind With 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.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the React cheatsheet.

← Back to all React questions
Pro · $10/mo

88 of 103 React answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime