How do you type event handlers in React with TypeScript?
TL;DR
React passes synthetic events typed by @types/react as React.<Kind>Event<ElementType>: React.ChangeEvent<HTMLInputElement>, React.MouseEvent<HTMLButtonElement>, React.KeyboardEvent<HTMLDivElement>, React.FormEvent<HTMLFormElement>, React.FocusEvent, React.PointerEvent, React.DragEvent, React.ClipboardEvent and so on. Inline handlers are inferred automatically, so you only write types for extracted handlers or callback props, where React.<Kind>EventHandler<El> or the event type is the right annotation. Know the difference between currentTarget (the element the handler is attached to, typed by the generic) and target (whatever was actually clicked, typed loosely as EventTarget).
Inline: no annotation needed
<input onChange={e => setName(e.target.value)} /> // e: React.ChangeEvent<HTMLInputElement>
<button onClick={e => e.currentTarget.blur()} /> // e: React.MouseEvent<HTMLButtonElement>
Extracted handlers
function SignupForm() {
const [email, setEmail] = useState('');
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setEmail(e.target.value);
};
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
const data = new FormData(e.currentTarget);
// ...
};
const handleKeyDown: React.KeyboardEventHandler<HTMLInputElement> = e => {
if (e.key === 'Escape') e.currentTarget.blur();
};
return (
<form onSubmit={handleSubmit}>
<input value={email} onChange={handleChange} onKeyDown={handleKeyDown} />
</form>
);
}
React.KeyboardEventHandler<T> is shorthand for (event: React.KeyboardEvent<T>) => void.
Tip: hover over the onChange prop in your editor to see the exact expected type, then copy it.
target vs currentTarget
function List() {
const handleClick = (e: React.MouseEvent<HTMLUListElement>) => {
e.currentTarget; // HTMLUListElement
const li = (e.target as HTMLElement).closest('li'); // target is EventTarget; narrow it
if (li) console.log(li.dataset.id);
};
return <ul onClick={handleClick}>{/* ... */}</ul>;
}
For event delegation, narrow target with instanceof (if (e.target instanceof HTMLButtonElement)) instead of casting.
One handler for several inputs
const handleField = (e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement>) => {
const { name, value } = e.target;
setForm(f => ({ ...f, [name]: value }));
};
Callback props: expose data, not events
type SearchProps = { onSearch: (query: string) => void }; // better than (e: ChangeEvent) => void
Passing domain values keeps components decoupled from DOM details and easier to test.
Native vs synthetic events
e.nativeEventgives the underlying DOM event (MouseEvent,InputEvent, etc.).useEffectlisteners attached withaddEventListeneruse DOM types (KeyboardEvent, notReact.KeyboardEvent):
useEffect(() => {
const onKey = (e: KeyboardEvent) => { if (e.key === 'Escape') close(); };
window.addEventListener('keydown', onKey);
return () => window.removeEventListener('keydown', onKey);
}, [close]);
- React 17+ attaches listeners at the root container, and
e.persist()is a no-op (there's no more event pooling).
Pitfalls
- Using
React.ChangeEvent<HTMLElement>: thene.target.valuedoesn't exist. Use the specific element type. - Confusing
React.MouseEventwith the DOM's globalMouseEvent(an import or naming mix-up). - Typing
onChangefor a custom component as a DOM event when callers only need a value. FormEventvsSubmitEvent: React'sonSubmitusesReact.FormEvent. The nativesubmitteris one.nativeEvent(asSubmitEvent).
In an interview, mention…
React.XxxEvent<Element>types, and that inference handles inline handlers.XxxEventHandler<El>aliases for extracted handlers.currentTargetvstarget, and narrowing withinstanceof.- DOM types for
addEventListener, and callback props that pass values rather than events.
Spotted a mistake or a rights concern? Report an issue