Type the onChange event of an element in React (TypeScript)

avatar

Borislav Hadzhiev

Last updated: Apr 17, 2022

banner

Photo from Unsplash

Type the onChange event of an element in React (TypeScript) #

To type the onChange event of an element in React, set its type to React.ChangeEvent<HTMLInputElement>. The ChangeEvent type has a target property which refers to the element. The element's value can be accessed on event.target.value.

App.tsx
import {useState} from 'react'; const App = () => { const [message, setMessage] = useState(''); // 👇️ type as React.ChangeEvent<HTMLInputElement> // or React.ChangeEvent<HTMLTextAreaElement> (for textarea elements) const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => { setMessage(event.target.value); console.log(event.target.value); }; return ( <div> <input type="text" id="message" name="message" onChange={handleChange} value={message} /> </div> ); }; export default App;

We typed the event as React.ChangeEvent<HTMLInputElement> because we're typing an onChange event on an input element.

If you have a textarea element, you would type the event as React.ChangeEvent<HTMLTextAreaElement>.

The easiest way for you to find out what the type of an event is, is to write the event handler inline and hover over the event parameter in the function.
App.tsx
import {useState} from 'react'; const App = () => { const [message, setMessage] = useState(''); // 👇️ event is written inline // hover over the `event` parameter with your mouse return ( <div> <input type="text" id="message" name="message" onChange={event => console.log(event)} value={message} /> </div> ); }; export default App;

get type of change event inline

When the event is written inline, I can hover over the event parameter and it shows me what the type of the event is.

TypeScript is able to infer the type of the event when it's written inline.

This is very useful because it works with all events. Simply write a "mock" implementation of your event handler inline and hover over the event parameter to get its type.

Once you know what the type of the event is, you are able to extract your handler function and type it correctly.

Let's look at an example that uses the same approach to get the type of an onChange event of a textarea element.

App.tsx
import {useState} from 'react'; const App = () => { const [message, setMessage] = useState(''); // 👇️ event is written inline // hover over the `event` parameter with your mouse return ( <div> <textarea id="message" name="message" onChange={event => console.log(event)} value={message} /> </div> ); }; export default App;

get type of change event textarea

All we had to do to get the type of the onChange event is - write it inline on the element and hover over the event parameter.

Now that we have the type, we can extract the event handler into a function and type it correctly.

App.tsx
import {useState} from 'react'; const App = () => { const [message, setMessage] = useState(''); const handleChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => { setMessage(event.target.value); console.log(event.target.value); }; return ( <div> <textarea id="message" name="message" onChange={handleChange} value={message} /> </div> ); }; export default App;
Note that you can use this approach to get the type of all events, not just onChange events.

As long as you write the event handler function inline and hover over the event parameter, TypeScript will be able to infer the event's type.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.