This is what I am doing: jsfiddle
The critical section is:
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();
},
This works great right now. I put the content back before the component updates on the assumption that React leaves the DOM alone between updates and won't miss it. In fact, React seems to be fine with moving content (if I remove componentWillUpdate
and componentDidUpdate
, the positioned element still updates!)
My question is how many of the resulting assumptions are safe (i.e., if I assume these things, will my code break in a future version of React?):
- React does not care if DOM is moved around between updates as long as you put it back in
componentWillUpdate
.
- React event handlers will still work on elements that have been moved.
- React will merge any inline styles you ask it with styles already on the element (even if it did not set them.)
- React will update DOM it has rendered, even if you move that DOM somewhere else in the document!
The last one seems somewhat extreme and magical to me, but has some great implications if it holds.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…