TypeScript ReactDOM属性'类型'组件上缺少

时间:2016-04-21 21:33:18

标签: reactjs compiler-errors typescript

收到以下错误:

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

获得以下代码打字稿代码:

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);

我尝试了以下内容:

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

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

1 个答案:

答案 0 :(得分:4)

应该是这样的:

interface IProps{}    
interface IState {}

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

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

您没有将组件类传递给render函数,您需要传递实例。

修改

此:

var component = SomeComponent;

将类SomeComponent的引用分配到component变量中,这意味着如果您这样做,那么您可以同时执行这两项操作,它们是相同的:

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

如果你不想使用jsx / tsx表示法,那么在构建组件时你需要传递道具:

var component = new SomeComponent(props);