TypeScript Event Types and Event Handling in React: A Complete Guide for Beginners

nishanthan-k
2 min readNov 15, 2024

--

Handling Event Types in TypeScript: A Quick Guide

Handling events in TypeScript, especially in a React application, can feel a bit daunting at first due to the need for typing every event correctly. However, once you get familiar with the main types and their use cases, it becomes a breeze! This guide walks you through the most common event types in TypeScript for form elements like <input>, <select>, <textarea>, and more.

Why Type Event Handlers?

TypeScript helps us avoid errors and unexpected behavior by making sure we’re using events correctly. By defining the event types, we get:

  • Autocomplete support when writing event handlers.
  • Error prevention by catching mistakes at compile time.
  • Improved readability by clearly showing what type of event is being handled.

Common Event Types for Form Elements

1. The Basics: onChange, onBlur, and onFocus

Most form elements use the following event types:

  • onChange: Triggered when the value of an element changes.
  • onBlur: Triggered when the element loses focus.
  • onFocus: Triggered when the element gains focus.

Each of these events has a specific TypeScript type, which we’ll cover for each element.

Event Types for Different Form Elements

Input Elements (<input>)

<input> elements come in various types, such as text, checkbox, radio, and more. Here are some common event types:

Input TypeTypeScript Event TypeDescriptionText, Email, etc.React.ChangeEvent<HTMLInputElement>Triggered when text changes.CheckboxReact.ChangeEvent<HTMLInputElement>Triggered when checkbox is toggled.RadioReact.ChangeEvent<HTMLInputElement>Triggered when radio is selected.

Example usage:

const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const value = event.target.value;
console.log(value);
};

Textarea (<textarea>)

The textarea element shares similar events to input elements, but with its own specific type.

EventTypeScript Event TypeDescriptiononChangeReact.ChangeEvent<HTMLTextAreaElement>Triggered when the text changes.

Example usage:

const handleTextareaChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
const value = event.target.value;
console.log(value);
};

Select (<select>)

For <select> elements, you’ll use React.ChangeEvent<HTMLSelectElement> to handle changes in selection.

EventTypeScript Event TypeDescriptiononChangeReact.ChangeEvent<HTMLSelectElement>Triggered when an option is selected.

Example usage:

const handleSelectChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
const selectedValue = event.target.value;
console.log(selectedValue);
};

Form Submission (<form>)

For form submission, you’ll use React.FormEvent<HTMLFormElement>. This is handy for handling form submissions and preventing the default behavior.

EventTypeScript Event TypeDescriptiononSubmitReact.FormEvent<HTMLFormElement>Triggered when the form is submitted.

Example usage:

const handleFormSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
// Process form submission
};

Button Events (<button>)

Buttons trigger onClick events, using the type React.MouseEvent<HTMLButtonElement>.

EventTypeScript Event TypeDescriptiononClickReact.MouseEvent<HTMLButtonElement>Triggered when the button is clicked.

Example usage:

const handleButtonClick = (event: React.MouseEvent<HTMLButtonElement>) => {
console.log("Button clicked!");
};

Wrapping Up

By specifying the correct event types, you gain TypeScript’s support to help prevent errors and write cleaner code. Here’s a quick summary of the most common event types:

  • Input, Checkbox, Radio: React.ChangeEvent<HTMLInputElement>
  • Textarea: React.ChangeEvent<HTMLTextAreaElement>
  • Select: React.ChangeEvent<HTMLSelectElement>
  • Form: React.FormEvent<HTMLFormElement>
  • Button: React.MouseEvent<HTMLButtonElement>

With these event types in your toolkit, handling events in TypeScript becomes much easier and more reliable. Now, go ahead and make your forms type-safe and robust!

--

--

nishanthan-k
nishanthan-k

Written by nishanthan-k

Data-driven professional with a passion for transforming complex information into insights. Expert in data analysis, visualization, and storytelling.

No responses yet