Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
630 views
in Technique[技术] by (71.8m points)

reactjs - React: How much can I manipulate the DOM React has rendered?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm a React dev. I'll answer each of your questions:


React does not care if DOM is moved around between updates as long as you put it back in componentWillUpdate.

True -- React doesn't look at the DOM except when updating, with the exception of event handlers:

React event handlers will still work on elements that have been moved.

I wouldn't rely on this, and I'm not sure that it's even true now.

React will merge any inline styles you ask it with styles already on the element (even if it did not set them.)

I also wouldn't rely on this -- right now React sets individual properties but I could easily imagine it setting el.style.cssText if that were faster, which would blow away individual changes that you've made.

React will update DOM it has rendered, even if you move that DOM somewhere else in the document!

I don't believe this is true currently and you also shouldn't rely on this.


Broadly speaking, you shouldn't manipulate by hand the DOM that React has created. It's 100% kosher to create an empty <div> in React and populate it by hand; it's even okay to modify the properties of a React-rendered element as long as you don't later try to change its properties in React (causing React to perform DOM updates), but if you move an element, React may look for it and get confused when it can't find it.

Hope that helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...