简体   繁体   English

在React中通过TypeScript进行解构赋值

[英]Destructuring assignment via TypeScript in React

How should I do something like 我该怎么办呢

class App extends React.Component {
    render() {
      const { x, y, z} = this.props;
      return (...)
    }
}

in TypeScript? 在TypeScript? For now tslinter shows an Error: 现在tslinter显示错误:

Type 'Readonly<{ children?: ReactNode; 输入'Readonly <{children?:ReactNode; }> & Readonly<{}>' has no property 'x' and no string index signature. }>&Readonly <{}>'没有属性'x'且没有字符串索引签名。

You have to define a type for props: 你必须为props定义一个类型:

interface Props {
    x: string;
    y: number;
    z: string;
}

class App extends React.Component<Props, {}> {
    render() {
      const { x, y, z} = this.props;
      return (...)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM