The one thing that no one properly explains about React — Why Virtual DOM

2Comments

Write your comment…

So, does the vDOM do the WHOLE DOM? or just a specified singular element? I assume vDOM is based on something like MutationSummary (https://github.com/rafaelw/mutation-summary) for picking up changes to it? Furthermore is the vDOM essentially just a JSON/similar representation of the real DOM?
Is it possible to view/access the vDOM in react?

@saiki Thanks for the pointer! Unfortunately I did actually already create a DOM serialiser! Thanks for the interest too :). I just thought I might be barking up the wrong tree (see what I did there). Thanks again

Write a reply...

Have seen this "explanation" and diagram many times. But still, useless and actually doesn't explain everything.

  • When a Virtual DOM wants to what you call "rerender", it needs to use native DOM API itself because otherwise it is just impossible to communicate with the browser. Why I need a lot of extra layers here when I can do just A -> E instead of A -> B -> C -> D -> E with document.createDocumentFragment() for example?

  • When exactly a browser is doing "rerendering"? What about "forced reflow", "read first and write second"? Why you think modern browsers are not doing a lot of optimization already.

  • Any real business code examples where everyone can compare good vanilla JS DOM manipulation and same code with Virtual DOM? Please no more useless innerHTML in the loop. I've been loading and rerendering 10000+ comments with plain JS easily.

  • Any benchmarks?

These simple questions in normal situations developer can answer quickly, however in this "virtual" problem even core author of React couldn't answer any of them.

DOM isn't slow, you are - https://korynunn.wordpress.com/2013/03/19/the-dom-isnt-slow-you-are/

  • I understand the idea of vDOM. Any app today has changes in the UI but you don't need to for that to remove original body/section and place a modified body/section instead all the time. With vDOM you have a bit of less UI computations, but you now have new computations - you need to store in memory a huge document and each time search in it for changes. After that when vDOM will render into DOM browser still will do it job, you can't avoid it.
  • Can you provide a specification for sorting use case - What do you have, what do you want to do and what results are you expecting? I will write a vanilla JS sorting example and then we can compare. It's hard for me to browse so many .ts files to find what exactly it is doing :) In any case in real world you will never have so many operations at the same time and 2) screen size is very limitted, you don't need to do anything with 100 posts above I already scrolled, only when I can see changes +- some space, only then UI needs to change.
  • As a developer I always know what exactly must be changed, when and where. Yes createDocumentFragment() does the job when you add nodes but when you modify... I again need real examples of these complicated SPAs to be able to answer this. For each use case there is a simple solution. What exactly must be done? ...and it's not sorting a huge table every 10ms on my screen. Avarage human reaction is about 300ms. No more then one click per 300ms, no more then one action per 300ms.
  • What you call a React's idea to separate app into components is noway related and invented by React. It is a common and general way in software architecture and it was for many many years. It's hard to say who was first, but we can be sure that this idea is at least 25 years old since it is one of the core concepts in what is called a UNIX Philosophy. In frontend we actually had these components at least for 10 years of jQuery where each jQuery plugin or a subset from jQuery UI library - is a component. Talking about the data every app today inherits from 3-tier architecture (MVC ot MVwhatever) and again it is a very old principle and not connected to React. Every software has separation of business logic and presentation logic. User clicks on button, calls for action, XHR made, server reads DB, returns data, client puts this data in DOM. It always was unidirectional and Angular's 2-way data binding just blowed up the Internet with another buzzword and useless technic. In my vanilla apps I have a very small window.Api object after that I have a models directory with small ajax/API-speaking objects composed with window.Api and my usual vanilla reflow looks like:
    // like a comment
    btn.addEventListener('click', () => {
    const commentId = btn.dataset.id;
    // waiting for AJAX response, if there will be errors 
    // PostComment based on Api object will show a small alert
    PostComment.like(commentId).then(() => {
      // and if everything is ok, here we are doing our rerendering,
      // it's up to PostCommentUI object how to do that, 
      // in this case it could be just 
      // 1) incrementing .textContent (and NEVER InnerHTML) in some <span> where like count is stored
      // 2) mark a like button as active, something like btn.classList.add('active'), that's all
      PostCommentUI.like(commentId) 
    });
    });
    
Write a reply...

Join a friendly and inclusive Q&A network for coders

  • 🖥Pick the technologies you like & read great content through your feed.
  • 💬Ask a question when you want to learn more about anything.
  • 🚀Share what you know & build your portfolio.
Sign up nowLearn more

loading ...