我正在尝试在ReactJS中创建组件来创建PopUps但是我在react-with-addons.js中遇到了这个错误:
错误:不变违规:_registerComponent(...):目标容器不是DOM元素。
这是我的代码:
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="react-0.13.3/build/react-with-addons.js"></script>
<script type="text/javascript" src="react-0.13.3/build/JSXTransformer.js"></script>
<script type="text/javascript" src="react-router/lib/umd/ReactRouter.js"></script>
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css"/>
<link rel="stylesheet" href="bootstrap/css/bootstrap-theme.min.css"/>
<script type="text/javascript" src="jquery/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
<title>WS-JAVA-REST</title>
<style>
.row { padding-top: 10px; }
</style>
</head>
<body>
<!-- contenu -->
<div id="content">
<script type="text/jsx" src="test.js"></script>
</div>
</body>
</html>
的Javascript
/** @jsx React.DOM */
var clicked = function(){
mountedPopup.setState({visible: true, pretendStateChanged: Date.now() });
mountedComponent.setState({pretendStateChanged: Date.now() }) ;
}
var popupClicked = function(){
mountedPopup.setState({visible: false, pretendStateChanged: Date.now() });
mountedComponent.setState({pretendStateChanged: Date.now() }) ;
};
var Component = React.createClass({
getInitialState: function() {
return {pretendStateChanged: Date.now() };
},
render: function(){
return React.DOM.div(null,
React.DOM.a({onClick: clicked, href: "javascript:void(0);"}, "Show popup"),
React.DOM.br(null),
React.DOM.span(null, "State: " + this.state.pretendStateChanged)
);
}
});
var Popup = React.createClass({
getInitialState: function() {
return {visible: false, pretendStateChanged: Date.now()};
},
componentWillUpdate: function(nextProps, nextState){
if (!this.state.visible && nextState.visible) {
this.popUp();
}
/* closed by application */
if (this.state.visible && !nextState.visible) {
this.closePopUp();
}
},
popUp: function(){
var self = this;
var parent = this.getDOMNode().parentNode;
$.magnificPopup.open({
items: {
src: parent,
type: 'inline'
},
removalDelay: 30,
callbacks: {
afterClose: function() {
if (self.state.visible){
/* closed by user pressing ESC */
self.setState({visible: false});
}
}
}
});
},
closePopUp: function(){
$.magnificPopup.close();
},
render: function(){
return React.DOM.div(null,
React.DOM.a({
onClick: this.props.onClickHandler,
href: "javascript:void(0);"
}, "Button inside popup"),
React.DOM.br(null),
React.DOM.span(null, "State: " + this.state.pretendStateChanged)
);
}
});
var mountedComponent = React.render(
<Component/>,
document.body.childNodes[1]
);
var mountedPopup = React.render(
<Popup onClickHandler={this.popupClicked} />,
document.body.childNodes[3]
);
所以我完全错过了什么?为什么我收到此错误?有人可以帮帮我吗?
答案 0 :(得分:3)
您的问题的简短回答是原始组件尚未加载,因此没有目标DOM节点来加载Popup组件。 React.render有一个可选的第三个参数,用于在渲染完成时触发的回调。因此,以下内容适用于您的情况:
var mountedPopup;
var mountedComponent = React.render(
<Component/>,
document.body.childNodes[1],
function() {
mountedPopup = React.render(
<Popup onClickHandler={this.popupClicked} />,
document.body.childNodes[3]
);
}
);
然而,正如其他人所提到的,这段代码也有一些重要的建议: