A Short Note – Shadow DOM vs Virtual DOM in ReactJS

Hello Readers, CoolMonkTechie heartily welcomes you in A Short Note Series.

In this note series, we will understand about the difference between Shadow DOM and Virtual DOM in ReactJS. We discuss the key differences between the virtual DOM and the shadow DOM.

ReactJS Shadow DOM Vs Virtual DOM

So Let’s begin.

The shadow DOM and virtual DOM are both fundamental to progressive web frameworks like Angular, Vue, and React. While the shadow DOM encapsulates the implementation of custom web components, the virtual DOM is used to differentiate changes and more effectively re-render UIs.


What is the Virtual DOM ?

The virtual DOM is an in-memory representation of the real DOM. Popular UI libraries like React and Vue implement a virtual DOM to more efficiently re-render UI components based on a “diffing” process. By comparing changes between a virtual DOM and the real DOM, rendering engines can more efficiently determine what actually needs to be updated. This avoids unnecessary redrawing of DOM nodes as only elements that have changed are redrawn. Without the virtual DOM, every element is redrawn regardless of whether or not it has changed. This adds a huge performance boost to DOM manipulation since redrawing elements is an expensive process.


What is the Shadow DOM ?

The shadow DOM is a way of encapsulating the implementation of web components. Using the shadow DOM, you can hide the implementation details of a web component from the regular DOM tree. A popular example is the HTML5 slider input. While the regular DOM recognizes this as a simple <input/> tag, there is underlying HTML and CSS that make up the slide feature. This sub-tree of DOM nodes is hidden from the main DOM to encapsulate the implementation of the HTML5 slider. Additionally, the CSS properties for the slider are isolated from the rest of the DOM. This provides an isolated scope that prevents the component’s styles from overriding other CSS properties defined elsewhere.

The isolated scope provided by the shadow DOM results in performance benefits. By isolating the CSS properties for a custom web component, the browser can more accurately determine what needs to be updated when the DOM is manipulated.


Shadow DOM vs Virtual DOM

While the shadow DOM and virtual DOM are seemingly similar in their creation of separate DOM instances, they are fundamentally different. The virtual DOM creates an additional DOM. The shadow DOM simply hides implementation details and provides isolated scope for web components.


Conclusion

In this note series, We understood about the difference between Shadow DOM and Virtual DOM in ReactJS. We conclude that both the shadow DOM and virtual DOM have played a key role in the development of progressive web frameworks. While both add performance benefits, the virtual DOM is used to efficiently redraw UIs whereas the shadow DOM encapsulates the implementation of custom web components.

Thanks for reading ! I hope you enjoyed and learned about Shadow DOM Vs Virtual DOM Concepts in ReactJS. Reading is one thing, but the only way to master it is to do it yourself.

Please follow and subscribe us on this blog and and support us in any way possible. Also like and share the article with others for spread valuable knowledge.

If you have any comments, questions, or think I missed something, feel free to leave them below in the comment box.

Thanks again Reading. HAPPY READING !!😊😊😊

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s