如何处理打字稿中的 onChange

lo7*_*lo7 5 typescript reactjs material-ui tsx

我是打字稿的新手。使用 Typescript 语言时,您通常如何处理onChangein TextField

  • handleChangeDate(e: React.ChangeEvent<any>)下面代码中的函数有效,但我收到警告,因为我使用了类型any. 我可以用哪种其他方式编写此代码?

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);
};
Run Code Online (Sandbox Code Playgroud)

kei*_*kai 9

对于 MUI 文本字段

event: React.ChangeEvent<HTMLInputElement>
Run Code Online (Sandbox Code Playgroud)

  • 我收到以下错误:“TS2345:‘字符串’类型的参数不可分配给‘SetStateAction’类型的参数。” (2认同)