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.js
Keeping 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.
The
App
component is being imported in fromsrc/App.js
Remember, thedefault
part ofexport default App
insrc/App.js
means that importing other names - likeApp
- actually already brings in theApp
component! As a best practice, though, we're going to leave this file alone. We will importHello
inside of theApp
along with other components, such asDog
andHuman
. Pretty cool!!
The last difference is that ReactDOM.render( <App />,
.
This changes the
ReactDOM.render()
call to explicitly say "Render whatever the componentApp
returns."
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
App
component name that's imported to be yourHello
component.Change the component name that's used inside
ReactDOM.render
to be yourHello
component.
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:
<Hello />
uses the name of the component to render. In ourHello.js
file, theHello
component returns the content to render: adiv
with "Hello World!" and heading tags (written in JSX). As a reminder, this is theHello
component:
The second argument of the
ReactDOM.render()
function isdocument.getElementById('root')
; this finds the DOMelement
to 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.html
file 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