React.JS中的组件元素无效 [英] Invalid Component Element in React.JS

查看:109
本文介绍了React.JS中的组件元素无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我有以下代码。它似乎可以工作,如果我做React.createElement,但不适用于JSX元素。

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>


推荐答案

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

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

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

示例

jsx-in-深度

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

Or you can use React.createElement, like so

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

示例

这篇关于React.JS中的组件元素无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆