簡體   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