gpt4 book ai didi

javascript - React.JS 中的无效组件元素

转载 作者:行者123 更新时间:2023-12-03 13:08:31 24 4
gpt4 key购买 nike

我正在学习 React.js 教程,其中包含一个简单的 hello world 示例。我找不到以下内容不起作用的原因,但我不断收到此错误。

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

关于javascript - React.JS 中的无效组件元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30275793/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com