Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Next.js and React

A React handler reads event.target.name and gets undefined

Your error

event.target.name is undefined2 more lines it can show

Fix it fast

Most common cause, 1 of 2

The event started on a child element

Read the name from e.currentTarget.

Fix
function Toolbar({ onAction }: { onAction: (name: string) => void }) {
  const handleClick = (e: React.MouseEvent<HTMLButtonElement>) => {
    const { name } = e.currentTarget; // the button, never the icon
    onAction(name);
  };
  return (
    <button name="save" onClick={handleClick}>
      <SaveIcon /> Save
    </button>
  );
}
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

event.target.name is undefined when event.target is not the element that carries the name. A click on an icon or span inside a button makes target that child, so read e.currentTarget.name instead. Library inputs such as MUI's Autocomplete pass the new value as a separate argument, so take it from there and name the field yourself. Applies to React 19.

  • Applies to React 19.3, MUI 9.5 (Autocomplete, Select)
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Log both targets inside the handler and click the exact spot that fails. A child node as target with your button or input as currentTarget points to cause 1. A handler that belongs to a library component points to cause 2.

Run this first
const handle = (e) => {
  console.log('target:', e.target, 'currentTarget:', e.currentTarget);
};

Then find the line you see in this list and open its fix:

  1. 1The event started on a child elemente.target is an <svg>, <path>, <span> or <img> inside the button, link or label that has the name, while e.currentTarget is the element itself
  2. 2A component library passes its own event and the valueThe handler is the onChange of a library component such as MUI Autocomplete or Select, and e.target is an option element or an object that holds a value but not your field name

What does the error look like?

  • event.target.name is undefined
  • e.target logs an <svg>, <path> or <span> instead of the button with the name
  • event.target.name is undefined in the onChange of an MUI Autocomplete or Select

Cause 1 of 2

The event started on a child element

You seee.target is an <svg>, <path>, <span> or <img> inside the button, link or label that has the name, while e.currentTarget is the element itself

Check

event.target.name is undefined here because React sets target to the node where the event happened, which can be a distant child, and currentTarget to the node your handler is attached to. A click on the icon inside <button name="save"> lands on the icon, and the icon does not carry the name you gave the button.

Check
console.log(e.target.tagName, e.currentTarget.tagName)

Fix

Read the name from e.currentTarget. Copy what you need into consts at the start of the handler, because currentTarget is only meant to be read while the handler runs; code after an await or inside a timer finds it null.

Fix
function Toolbar({ onAction }: { onAction: (name: string) => void }) {
  const handleClick = (e: React.MouseEvent<HTMLButtonElement>) => {
    const { name } = e.currentTarget; // the button, never the icon
    onAction(name);
  };
  return (
    <button name="save" onClick={handleClick}>
      <SaveIcon /> Save
    </button>
  );
}

Verify

Click the icon, then the label text of the button; both clicks report save.

Verify
console.log(e.currentTarget.name)

Confirmed by react.dev

Did this fix it?

Cause 2 of 2

A component library passes its own event and the value

You seeThe handler is the onChange of a library component such as MUI Autocomplete or Select, and e.target is an option element or an object that holds a value but not your field name

Check

With library inputs, event.target.name is often undefined because the event does not come from a named input of yours. MUI's Autocomplete calls onChange with the event and the new value as a second argument. Its Select documents the event as a generic one, not a change event, and points you to event.target.value for the new value.

Check
onChange={(e, value) => console.log(e.target, value)}

Fix

Use the value the library hands you and write the field name into the handler, instead of reading it back from the event.

Fix
<Autocomplete
  options={countries}
  onChange={(_event, value) => setForm((f) => ({ ...f, country: value }))}
  renderInput={(params) => <TextField {...params} label="Country" />}
/>

<Select
  value={form.size}
  onChange={(e) => setForm((f) => ({ ...f, size: e.target.value }))}
>
  <MenuItem value="s">Small</MenuItem>
</Select>

Verify

Pick an option; the matching state field changes and no code reads target.name any more.

Verify
console.log(form)

Confirmed by mui.com

Did this fix it?

How do I stop it coming back?

  • Type each handler with the element it is attached to, such as React.MouseEvent<HTMLButtonElement>, and read attributes from currentTarget.
  • For library inputs, pass the field name to a shared handler explicitly, for example handleField('country'), instead of relying on a name attribute.
  • Read event values into consts before any await, so nothing reads the event after the handler has returned.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, QistLock, Taskvare.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.