所以我有一個使用的骨幹路由器反應的應用程序,但是當我嘗試在DOMContentLoaded
導航,我得到:不變違規:目標容器不是一個DOM元素,當元素在DOM
Uncaught Error: Invariant Violation: _registerComponent(...): Target container is not a DOM element.
我嘗試了一下堆棧跟蹤,但無法弄清楚發生了什麼,因爲引發錯誤的方法(ReactMount._registerComponent
)被命中幾次,其中第一個不會拋出錯誤,因爲DOM有問題的元素在那裏。我使用我在其他項目中使用的組件,沒有問題,並剝奪所有作品降到最低調試這(失敗至今):
DOM結構:
<html>
<head>
</head>
<body>
<div id="app-container">
</div>
<script src="http://fb.me/react-with-addons-0.12.0.js"></script>
<script type="text/javascript" src="js/application.js"></script>
</body>
</html>
與路由器代碼:
AppRouter.prototype.signIn = function() {
var container = document.getElementById('app-container');
React.render(
<LoginForm />,
container
);
};
LoginForm的組分:
var LoginForm = React.createClass({
render: function() {
return(
React.render(
<div id="login-form-component">
</div>
)
);
},
});
路線被命中,按預期擊中- 我錯過了什麼?
這裏是堆棧跟蹤,其底部是我的路由器登入方法:
invariant
ReactMount._registerComponent
(anonymous function)
ReactPerf.measure.wrapper
ReactMount.render
ReactPerf.measure.wrapper
React.createClass.render
(anonymous function)
ReactPerf.measure.wrapper
(anonymous function)
ReactPerf.measure.wrapper
ReactComponent.Mixin._mountComponentIntoNode
Mixin.perform
ReactComponent.Mixin.mountComponentIntoNode
(anonymous function)
ReactPerf.measure.wrapper
ReactMount.render
ReactPerf.measure.wrapper
AppRouter.signin
感謝您的閱讀!
將'console.log(document.getElementById('app-container'))'放在.signIn中,只是爲了檢查。 99%的時間意味着你傳遞了undefined或null作爲第二個參數。 – FakeRainBrigand 2014-11-08 18:24:30
我在檢查後看到類似的問題,因此元素是:) – mattmattmatt 2014-11-08 18:31:44