whatoncewaslost
whatoncewaslost

Reputation: 2256

Invalid Component Element in React.JS

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>

Upvotes: 20

Views: 10472

Answers (1)

Oleksandr T.
Oleksandr T.

Reputation: 77482

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

Upvotes: 22

Related Questions