admin管理员组

文章数量:1203217

I've been going through a react.js tutorial, with a simple hello world example. 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.

<!doctype html>
<html>
<head>
    <script src=".13.3.js"></script>
    <script src=".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>

I've been going through a react.js tutorial, with a simple hello world example. 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.

<!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>
Share Improve this question asked May 16, 2015 at 12:39 whatoncewaslostwhatoncewaslost 2,2562 gold badges19 silver badges26 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 22

You should pass to render <HelloWorld />, not new HelloWorld()

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

Example

jsx-in-depth

Or you can use React.createElement, like so

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

Example

本文标签: javascriptInvalid Component Element in ReactJSStack Overflow