Typing onChange event in React and TypeScript: how to get event.target.value
Hello everyone! I'm new to TypeScript, migrating from plain JavaScript to React + TypeScript. I've encountered a problem when handling the onChange event in an input. I can't figure out how to properly type the event.
Here's my code:
```tsx
import React, { useState } from 'react';
const MyForm: React.FC = () => {
const [value, setValue] = useState('');
const handleChange = (event) => {
setValue(event.target.value);
};
return (
);
};
```
TypeScript complains about the `event` parameter — it says it has an implicit `any` type. I tried writing `event: any`, but that's bad practice, and the linter also complains. I tried searching online, but everywhere there are different options: some use `React.
No answers yet.
Log in to leave an answer
Blogs