Ben Ashkenazi
Ben Ashkenazi

Reputation: 103

Where does "this.state" comes from when using TypeScript in React

I was wondering where does the property this.state comes from when using React with TypeScript?

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

We are not importing it in the super() call, so where does it comes from?

Upvotes: 0

Views: 400

Answers (2)

Dennis Vash
Dennis Vash

Reputation: 53884

It comes from the internal implementation of React component which you inherit by extending the class React.Component:

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

See React.Component and its instance properties.

Upvotes: 1

Code-Apprentice
Code-Apprentice

Reputation: 83527

where does it comes from?

It comes from the constructor. The line you are asking about is exactly where this.state is initialized.

Upvotes: 0

Related Questions