A React handler reads event.target.name and gets undefined
Fix it fast
Most common cause, 1 of 2The event started on a child element
Read the name from e.currentTarget.
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>
);
}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.
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.
const handle = (e) => {
console.log('target:', e.target, 'currentTarget:', e.currentTarget);
};Then find the line you see in this list and open its fix:
- 1The event started on a child element
e.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 - 2A component library passes its own event and the value
The 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 undefinede.target logs an <svg>, <path> or <span> instead of the button with the nameevent.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.
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.
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.
console.log(e.currentTarget.name)Confirmed by react.dev
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.
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.
<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.
console.log(form)Confirmed by mui.com
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.