当我尝试将 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/