Virtual DOM
Learning Objectives
After this lesson, you will be able to:
Describe the Virtual DOM versus the standard DOM
Understand how components are called
Review src/index.js
src/index.jsKeeping components separate and organized is a best practice, so we created the Hello component in its own file.
To show up on the page, though, that component still needs to actually be called from somewhere. The main "HUB" 🛖 of our React app is src/index.js. We'll investigate how src/index.js is currently loading and rendering the App component. Look at your src/index.js file, and contrast it with the code below.
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.js';
ReactDOM.render(
<App />,
document.getElementById('root')
)The
Appcomponent is being imported in fromsrc/App.jsRemember, thedefaultpart ofexport default Appinsrc/App.jsmeans that importing other names - likeApp- actually already brings in theAppcomponent! As a best practice, though, we're going to leave this file alone. We will importHelloinside of theAppalong with other components, such asDogandHuman. Pretty cool!!
The last difference is that ReactDOM.render( <App />,.
This changes the
ReactDOM.render()call to explicitly say "Render whatever the componentAppreturns."
Let's Hack away!
Code along: Calling our Hello component explicitly
Hello component explicitlyUpdate your index.js file to have the three changes listed above:
Delete the CSS import.
Change the
Appcomponent name that's imported to be yourHellocomponent.Change the component name that's used inside
ReactDOM.renderto be yourHellocomponent.
Check it out! You should be able to browse to http://localhost:3000 and see that nothing has changed.
Virtual DOM Intro
You should be familiar with the DOM. You may have noticed that our src/index.js code mentions ReactDOM. ReactDOM doesn't refer to the same DOM we know. Instead, it refers to a Virtual DOM. The Virtual DOM is a key piece of how React works.
So, how is different? Watch this video to find out. (note: right click for new tab!)
In React, the virtual DOM is a staging area for changes that will eventually be implemented.
You know every component has, at a minimum, a return statement. The return generates a Virtual DOM node to be added to the actual DOM.
The contents of this node are what we define in the return statement, using JSX.
The ReactDOM.render() function takes two arguments:
ReactDOM.render(
<Hello />,
document.getElementById('root')
)<Hello />uses the name of the component to render. In ourHello.jsfile, theHellocomponent returns the content to render: adivwith "Hello World!" and heading tags (written in JSX). As a reminder, this is theHellocomponent:
import React from 'react';
import Hello from './Hello';
function App() {
return (
<div>
<Hello />
</div>
);
}
export default Hello;The second argument of the
ReactDOM.render()function isdocument.getElementById('root'); this finds the DOMelementto append that content to. This argument can be any element on the page. Here, we're simply appending it to an element with the idroot. (Look through theindex.htmlfile if you're curious about the HTML structure fromcreate-react-app.)
When our index.js is processed, React compares the Virtual DOM to the regular DOM and only updates the root element on the page. Dope!
Side note: What is
<Hello />written in? JSX! Whenever you use a self-closing tag in JSX, you MUST end it with a/, like<Hello />in the above example. If you don't use a self-closing tag, JSX will look for a closing tag and never find it!
Let's switch it back to
<App />!
If you're you have 35 mins of nothing to do on the weekend, check out this video with history on the Virtual to learn more, check this video out. (note: right click for new tab!)
Last updated
Was this helpful?