React.JS中的组件元素无效

Oce*_*uto 20 javascript reactjs

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

Uncaught Error: Invariant Violation: React.render(): Invalid component element.
Run Code Online (Sandbox Code Playgroud)

我有以下代码.它似乎可以工作,如果我做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>
Run Code Online (Sandbox Code Playgroud)

Ale*_* T. 21

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

React.render(<HelloWorld />, document.getElementById("test"))
Run Code Online (Sandbox Code Playgroud)

Example

jsx-in-depth

或者你可以使用React.createElement,像这样

React.render(React.createElement(HelloWorld, null), document.getElementById("test"))
Run Code Online (Sandbox Code Playgroud)

Example