JSX and Virtual DOM are the two main concepts of React JS that powered React JS to reach its unique stage. Today we will discuss these two concepts in brief.
Before going into detail about why JSX is used, let’s take a look at how HTML renders by the browsers.
We all know that browsers can only understand HTML code. But how does the browser run the HTML code under the hood and display the amazing pages? Basically, when the browser renders the HTML code, it passes them through HTML parser and creates an object-like structure called DOM tree (DOM stands for Document Object Model). The next step of rendering is Attachment. In the attachment phase, all the style rules are attached with DOM tree and sent to the Render tree. Finally, Render tree pains the element on the screen, and we can see them.
When browser converts the HTML to DOM tree, it used a document method called createElemet() for every HTML element. For example -
const root = document.getElementById("root"); const h2 = document.createElement("h2"); h2.innerText = "Hello, world!"; root.appendChild(h2);
In the above code, first, we have created an HTML element using document.createElement() method. The browser does the same for every HTML element on the page while parsing a page. When browser’s HTML parser finds any HTML element, it converts it to DOM element using document.createElement method. So, HTML is nothing but a syntactic sugar of createElement method, which allows us to create elements simply and concisely.
Similarly, React JS has a virtual DOM. It needs to create React Elements for its virtual DOM. We can create react elements using React.createElement method. But it is tedious to create multiple or nested elements by calling the React.createElement method again and again. JSX made a developer’s life easy and simple by enabling us to create react elements using simple HTML-like syntax. See the following examples -
To display hello h2 (wrapped in h2) and hello h3 (wrapped in h3) on the web page under root div using React.createElement we have to write -
const root = document.getElementById("root"); // Only using React.createElement const element = React.createElement('div', null, [ React.createElement("h2", null, "Hello h2"), React.createElement("h3", null, "Hello h3"), ]); ReactDOM.render(element, root);
But we can do the same using JSX like the following -
const root = document.getElementById("root"); // Using JSX const element = <div> <h2>Hello h2</h2> <h3>Hello h3</h3> </div> ReactDOM.render(element, root);
We have discussed the browser DOM in short in the JSX section. React JS stands on an idea of something similar to browser DOM called virtual DOM. Virtual DOM is a mirror copy of browser DOM. When we run a react app in the browser, React JS creates a copy of the browser DOM and holds it in the memory.
The reason React creates a virtual DOM is to identify any change of state on the DOM elements and update it to the UI quickly and efficiently.
When we change any element in the browser DOM, it needs to re-render the whole DOM tree. Modern single-page applications can have hundreds of thousands of states. Sometimes, it is costly to detect any state change and update the UI accordingly. React brought a revolution in this case of handling vast amounts of state very quickly and efficiently.
How actually React JS handle this using virtual DOM? Well, let me explain.
As I have mentioned earlier, React creates a virtual representation of browser DOM when the application renders for the first time on the browser. If any of the elements or states change in the page, react create another copy of the previous virtual DOM without re-rendering the browser DOM and compare the changes between the previous virtual DOM and newly created virtual DOM using diffing algorithm. React made it very efficient and quick to find out the difference between them because there is no UI painting involved there. After identifying the difference, React only update the differents part of the browser DOM without re-rendering the whole page.
Though React is not the best solution for all use cases, it performs better compared with vanilla JS or using jQuery, where needs to deal with a massive number of states like SPA because of its virtual DOM concepts. So we should keep a clear-cut concept of React virtual DOM as a React JS developer.