reactjs不变违规

时间:2015-10-30 04:07:59

标签: reactjs

尝试在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>

1 个答案:

答案 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>