小编Vin*_*irk的帖子

Typescript & ReactJS 如何动态设置状态

我将此接口定义为我的状态:

interface State {
  id: string;
  name: string;
  description: string;
  dimensionID: string;
  file: File | null;
  operator: string;
  isFormValid: boolean;
  filename: string;
};
Run Code Online (Sandbox Code Playgroud)

我有一个简单的更改处理程序:

  update = (event: React.FormEvent<HTMLInputElement>): void => {
    const { name, value } = event.currentTarget;
    this.setState({ [name]: value });
  };
Run Code Online (Sandbox Code Playgroud)

然而,这个错误被抛出:

Error:(109, 19) TS2345: Argument of type '{ [x: string]: string; }' is not assignable to parameter of type 'State | Pick<State, "id" | "name" | "description" | "dimensionID" | "file" | "operator" | "isFormValid" | "filename"> …
Run Code Online (Sandbox Code Playgroud)

types typescript reactjs

8
推荐指数
1
解决办法
8238
查看次数

标签 统计

reactjs ×1

types ×1

typescript ×1