Hello World 👋
- Import React.js and React DOM libraries
- Create HTML
- Create React element
- See Hello World
Okay, okay, okay. Welcome to Module One: Baby Steps with React. So, first of all, let's build Hello World. Let's get it off our shoulders. Let's get it off our minds, because if we don't build Hello World, guess what? The gods of programming will frown upon us. :)
So, let's keep it simple, stupid. We can download the libraries, but we can also link them from CDNs. The result, the goal and the result is to just get the code. And, by linking or downloading the files, you would get the React and ReactDOM object as global, so window.react will be accessible to you.
And, after you have those includes in your index.html, what you can do, you can put the code right there inside of the file as well. Let's just keep it as simple, as stupid as possible for this Hello World. Later we will split them into multiple files. For now, let's keep it simple. Pay attention, this create tag must be after the div container. So, the div container with the ID content, that's where our React element will be mounted. This is very important. I will also remind you about this later in this module, about the order of the code.
Now, let me show you one more way how to do exactly the same thing, React.DOM.h1. So, inside of that React.DOM you have the list of all the objects. So, actually what create element is doing, basically create element and DOM.h1, they're aliases for each other. One is calling the other. So, DOM.h1 is a little bit shorter. In this case, there are only two arguments. Again, I will talk about the null a little bit later. Hello World, that's what we need for now.
So, okay, we have our elements, so now what do we need to do? It's not gonna show up on the page. We need to mount it. So, mounting, it's like linking that React element, which is so far has been just created as just an object. It's of a special type h1, but it's not gonna show up on the page, so what we need to do, we need to pick another library, ReactDOM, which if you remember, you hotlinked from the CDN, and then we would use this function, it's called render. And, we would pass an element, pass our h1 element, and then we would pass DOM node by using document.getElementById. And, this will actually enable us to see Hello World. Ta-dam! Congratulations, you just built your first React code. I wanted to say component, but it's not a component yet. It's just an element. We mounted it. Components will be in the second module. But, so far so good.