尝试在React中呈现一个简单的段落时,在每个浏览器上都出现此错误:
未捕获错误:不变违规:_registerComponent(...):目标 容器不是DOM元素。
以下是代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Basic Example</title>
<link rel="stylesheet" href="../shared/css/base.css" />
</head>
<body>
<div class="container">
<p>this should not be seen</p>
</div>
<script src="../../build/react.js"></script>
<script src="../../build/react-dom.js"></script>
<script>
"use strict";
var BoxTile = React.createClass({
render: function () {
return React.createElement("p", null, "Imageine this is a tile...");
}
});
ReactDOM.render(React.createElement(BoxTile), document.getElementById('container'));
</script>
</body>
</html>
答案 0 :(得分:2)
document.getElementById
无法找到相关节点,因为container
是一个类属性而非id。
更改
<div class="container">
<p>this should not be seen</p>
</div>
要
<div id="container">
<p>this should not be seen</p>
</div>