【问题标题】:reactjs Invariant Violationreactjs不变违规
【发布时间】:2016-01-30 10:28:37
【问题描述】:

尝试在 React 中呈现简单段落时,在每个浏览器上都出现此错误:

未捕获的错误:不变违规:_registerComponent(...): Target 容器不是 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>

【问题讨论】:

  • 请从标题中删除'ver 14',因为这个错误与React的版本无关。旧版本也会给你这个错误(或类似的东西)。

标签: reactjs


【解决方案1】:

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>

【讨论】:

  • 非常感谢,将其替换为 document.getElementsByClassName('container')[0] 并且工作正常
猜你喜欢
  • 2015-11-14
  • 2020-10-02
  • 2015-09-28
  • 2016-06-26
  • 1970-01-01
  • 2020-05-06
  • 2020-03-20
  • 1970-01-01
  • 2021-11-07
相关资源
最近更新 更多