这就是我正在做的:jsfiddle
关键部分是:
position: function() {
var container = $(this.getDOMNode());
this._menu = $(this.refs.menu.getDOMNode());
this._menu.appendTo(document.body).
offset({
top: container.offset().top +
container.outerHeight(),
left: container.offset().left
});
},
restore: function() {
this._menu.appendTo(this.getDOMNode());
},
componentWillUpdate: function() {
this.restore();
},
componentDidUpdate: function() {
this.position();
},
componentDidMount: function() {
this.position();
},
这个功能现在运行得很好。我在组件更新之前将内容放回原处,假设React在更新之间不会丢失DOM。事实上,React似乎可以很好地处理移动内容(如果删除componentWillUpdate
和componentDidUpdate
,定位的元素仍然会更新!)
我的问题是,有多少由此产生的假设是安全的(即,如果我假设这些事情,我的代码会在React的未来版本中崩溃吗?):
只要您将DOM放回componentWillUpdate
.
最后一个对我来说似乎有点极端和神奇,但如果它成立的话,会有一些很大的影响。
https://stackoverflow.com/questions/23530716
复制相似问题