首页 > 解决方案 > 在 React 中使用 TypeScript 时“this.state”从何而来

问题描述

this.state我想知道将 React 与 TypeScript 一起使用时该属性从何而来?

public constructor(props: any) {
    super(props);
    
    this.state = {
        userName:"Code to html, one way -->",
        password:""
    };
}

我们没有在 super() 调用中导入它,那么它是从哪里来的呢?

标签: reactjstypescript

解决方案


它来自您通过扩展类继承的 React 组件的内部实现React.Component

class MyComponent from React.Component {
   // this.setState, this.state,
   // lifecycle methods and more 
}

请参阅React.Component及其实例属性


推荐阅读