Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle onChange in Typescript

I am new to Typescript. How do you normally handle onChange in TextField, when using Typescript language?

  • The function handleChangeDate(e: React.ChangeEvent<any>) in the code below works, but I get warnings because I use the type any. Which other way can I write this code?

import React, { useState } from 'react';
import { useForm } from 'react-hook-form';
import { TextField } from '@material-ui/core';

const [date, setDate] = useState(
    new Date().getDate() + '/' + (new Date().getMonth() + 1) + '/' + new Date().getFullYear(),
);

const handleChangeDate = (e: React.ChangeEvent<any>): void => {
    setDate(e.target.value);
};
like image 385
lo7 Avatar asked Apr 08 '20 12:04

lo7


Video Answer


1 Answers

For MUI Textfield

event: React.ChangeEvent<HTMLInputElement>
like image 93
keikai Avatar answered Oct 13 '22 02:10

keikai