javascript - React 新手 => 元素类型无效 : expected a string

标签 javascript reactjs typescript

当我尝试将 React 应用程序的基础知识加载到浏览器中时,我正在绕圈子。

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.

代码如下(test.tsx)

    /// <reference path="./typings/tsd.d.ts" />

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


    ReactDOM.render(
            <TodoApp name="jim"/>, document.getElementById('root')
    );

    class TodoApp extends React.Component<any, any>{
        constructor(props) {super(props); this.state = {};}

        render() {
            return (
                <div>
                    <button>hit me</button>
                </div>
            )
        }
    }

export default TodoApp;

测试.html

<html>
    <body>
    <div id="root"></div>
    <script src="./bundle.js"></script>      
    </body>
</html>

tsx 已编译并与 Browserify 捆绑,但错误发生在bundle.js

特别是@ instantiateReactComponent(node) 和不变函数

提前非常感谢!

最佳答案

您正在调用 render 并在定义组件之前传入该组件。

// won't work!
var person = new Person() <-- undefined
class Person {}

// will work
class Person {}
var person = new Person()

类不会像函数那样被“提升”。

关于javascript - React 新手 => 元素类型无效 : expected a string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38111509/

相关文章:

typescript - 如何从对象生成点对象路径类型?

typescript 折线图 d3

javascript - 关闭模态时位置标记消失

javascript - 未捕获的语法错误 : Unexpected token u with Ajax post

reactjs - 单击覆盖层时 react 模式未关闭

javascript - react : Impact of conditional rendering on performance

javascript - Google Analytics - 行为 -> 事件未显示事件总数

javascript - 需要这个 javascript 的解释

javascript - 将脚本标签添加到 React/JSX

javascript - 无法将图像高度和宽度分配给 typescript 中的输入类型框