Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSam Katz
2,986 PointsIn what way does REACT encourage you to manipulate the DOM directly?
It seems the video argues that the virtual DOM and the DOM are kept in sync by REACT itself.
1 Answer
gqtojlonge
1,716 PointsHi Sam, Basically virtual dom is more efficient way of manipulating the dom in terms of speed. Without virtual dom, browser gets the html file and parse it. Then it goes through the process of creating so called dom nodes.Then it is calculating the styles(css) of each element.The third step would be layout( every node/element) is given screen coordinates(where it should appear on screen), and finally it should do the last step(painting those nodes on the screen). As you se from above browser needs to do quite few steps just to show element on page.When you do an update of html element browser is repeating all of those steps above(you can see here why it is less efficient than virtual dom).
Say you modified 100 nodes, one by one. That would mean 100 (potential) re-calculations of the layout, 100 (potential) re-renderings, etc..
But when you do same thing on virtual dom, al those changes are done on separate dom(virtual one) and only those changes are dumped to the real dom(more efficient).
If you need to manipulate real dom nodes in react, you can always use "REFS". Ref is nothing but a reference to a node, or instance of an component in your application. You will provide ref="someName" on an html element(for example input) : <input ref="username" type="text"/> Then you can access native elements functions(attributes) by calling this.refs.username.value (just an example)