4

Getting the following error:

Error   TS2345  Argument of type 'typeof SomeComponent' 
is not assignable to parameter of type 'ReactElement<{}>'.
Property 'type' is missing in type 'typeof SomeComponent'.

Got the following code typescript code:

import * as React from 'react';
import * as ReactDOM from 'react-dom';

interface IProps{}    
interface IState {}

class SomeComponent extends React.Component<IProps, IState> {
    render() {
        return <div />;
    }
}

var component = SomeComponent;
var root = document.getElementById("root");

ReactDOM.render(component , root);

I tried the following:

class SomeComponent extends React.Component<IProps, IState> {
    render() {
        return <div />;
    }

    get type() {
        return "test";
    }
}

1 Answer 1

5

Should be something like:

interface IProps{}    
interface IState {}

class SomeComponent extends React.Component<IProps, IState> {
    render() {
        return <div />;
    }
}

ReactDOM.render(<SomeComponent /> , document.getElementById("root"));

You don't pass the component class to the render function, you need to pass the instance.


Edit

This:

var component = SomeComponent;

Assigns a reference to the class SomeComponent into the component variable, meaning that if you do that then you can do both and they are the same:

var componentInstance1 = <SomeComponent />
var componentInstance2 = <component />

If you don't want to use the jsx/tsx notation then when constructing the component you need to pass the props:

var component = new SomeComponent(props);
Sign up to request clarification or add additional context in comments.

8 Comments

Doesn't this make it an instance? var component = SomeComponent;
Will ask another question why.
No. SomeComponent is a class not an instance of that class. Usually you'll get an instance with the new keyword (new SomeComponent()) but you use jsx so <SomeComponent /> does that work for you
lol. Ok I get it, so var x = SomeComponent, sets the typeof(SomeComponent) to var x. so was missing ` var component = new SomeComponent();` Thanks!
Will mark it as answer in 4 min once i can (time limit)
|

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.