简体   繁体   English

React.JS中的组件元素无效

[英]Invalid Component Element in React.JS

I've been going through a react.js tutorial, with a simple hello world example. 我一直在通过一个简单的hello world示例来阅读react.js教程。 I can't find a reason why the following shouldn't work, but I keep getting this error. 我找不到以下不应该工作的原因,但我不断收到此错误。

Uncaught Error: Invariant Violation: React.render(): Invalid component element.

I have the following code. 我有以下代码。 It seems to work if I do React.createElement, but not for the JSX elements. 它似乎可以工作,如果我做React.createElement,但不适用于JSX元素。

<!doctype html>
<html>
<head>
    <script src="https://fb.me/react-0.13.3.js"></script>
    <script src="https://fb.me/JSXTransformer-0.13.3.js"></script>
    <script type="text/jsx">
        document.body.onload = function(){
            console.log("shuff")
            var HelloWorld = React.createClass({
                render: function(){
                    return <div>Hello, Ian Shuff!</div>;
                }
            });

            React.render( new HelloWorld(), document.getElementById("test"))
        }

    </script>
</head>
<body>
    <div id="test"></div>
</body>
</html>

You should pass to render <HelloWorld /> , not new HelloWorld() 你应该传递给渲染<HelloWorld /> ,而不是new HelloWorld()

React.render(<HelloWorld />, document.getElementById("test"))

Example

jsx-in-depth

Or you can use React.createElement , like so 或者您可以使用React.createElement ,就像这样

React.render(React.createElement(HelloWorld, null), document.getElementById("test"))

Example

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM