1

I have recently started working with React and getting understanding of how the framework works,

I have two pieces of code below , just wondering that what's the difference between them (of-course I am not asking about the syntactical differences) and why one of them gives error.

this one works

interface Square {
  value:String;
}

class Square extends React.Component<Square,{}> {

}

this one gives following error

[ts] Generic type 'Component' requires 2 type argument(s).

class Square extends React.Component {}

I have seen many examples on the net which extend React.Component for writing new components, I think I am missing something here.

1 Answer 1

2

Because you are using TypeScript.

The code you saw on the internet class xxx extends React.Component is just ES6 code.

Here is a simple React code written in TypeScript:

interface SomeProps {
    blabla: string;
}

class SomeComponent extends React.Component<SomeProps, any> {
    constructor(props: SomeProps) {
        super(props);
    }

    render() {
        return <h1>{this.props.blabla}</h1>;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.