繁体   English   中英

React.JS中的组件元素无效

[英]Invalid Component Element in React.JS

我一直在通过一个简单的hello world示例来阅读react.js教程。 我找不到以下不应该工作的原因,但我不断收到此错误。

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

我有以下代码。 它似乎可以工作,如果我做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>

你应该传递给渲染<HelloWorld /> ,而不是new HelloWorld()

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

Example

jsx-in-depth

或者您可以使用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