36
loading...
This website collects cookies to deliver better user experience
Here we will try to explore Virtual DOM using React components because what's better than finding an answer in the question itself.
const DummyComponent = ({title}) =>{
return(
<div className="container">
<h1>{title}</h1>
<h3>This is a dummy component</h3>
</div>
)
}
console.log(DummyComponent("Calling the dummy component"))
Manipulating Virtual DOM is faster as compared to manipulating the actual DOM, the reason being that the Virtual DOM is only present in memory and can easily be created or deleted.
<button id="btn">Click me!</button>
, then we first need to have access to this element using the getElementById('btn')
, this method will traverse through the complete DOM and will find the element with the specific ID and then will perform the DOM manipulation using other traversing cycle. This process is simple if we have to deal with a small number of elements but let's say we have an application to view stock market trends, prices of various stocks, and related news, now in this application, we have thousands of components, the data inside them may change a couple of times within a second, and so handling this using simple DOM manipulation technique might be a tedious and time taking task. To overcome this drawback, React uses Virtual DOM for its DOM manipulation process.componentWillUnmount()
lifecycle method which removes it, after this the new component that has to be pained receives componentWillMount()
and componentDidMount()
that brings the new component to the frontend. This process of syncing Virtual DOM to the real DOM is acknowledged as Reconciliation.