<HelloReact/> Day 1 — React from Scratch — 30 Days of React

Would you like to get started learning the basics of react without getting bogged down creating a development environment?

Chances are that if you are new to web development that setting up a development environment can leave you feeling a little intimidated when you are just trying to learn React or just learn about React for the first time.

In this article we are going to look at how we can get started with React using only a text editor and a browser and nothing else.

1. Set Up Boiler Plate Code with Emmet

Let’s get started with step 1. We’ll begin with a file in our browser called “index.html”. We’ll begin with the boiler plate code HTML code. For a quick start I recommend using Emmet with whatever text editor you have and on the first line typing in html:5 then pressing the shift key to get the code below. Or you can go ahead and copy and paste the code from below.

We can fill in the title of “Let’s React!”.

<title>Let's React</title>

This content will not appear in your webpage. Anything in the head section of the HTML file will be meta data that our browser will user to interpret our code in the body section. This title is going to be what appears on the tab for our page, not actually on the page.

2. Get Script Tags to Harness the Power of React and Babel Libraries

Ok, item one is checked off of our list. Let’s look at item two. We are going to set up our developer environment by using script tags to bring in React and Babel. This is not a real life developer environment. That would be quite an elaborate setup. It would also leave us with a lot of boiler plate code and libraries that would take us off subject of learning React basics. The goal of this series is to go over the basic syntax of React and get right into coding. We are going to use

3. Render React to the DOM

Our next two steps will be to choose our location within DOM that we want to render our react content. And using another script tag for our React content within the body. Generally, as a good separations of concerns practice this would be in its own file then linked to this html document. We’ll do that later in upcoming lessons. For now, we’ll let this dwell within the body of the html document we are currently in. Now we are going to look at how simple it is to choose a place on the DOM to render our React content. We’ll go within the body. And best practice isn’t just to throw React into the body tag to be displayed but to create a separate element, often a div, that you can treat as a root element to insert your React content.

<body>
  <div id="app">React has not rendered yet</div>
</body>

We’ll create a simple <div> element and give it an id of app. We are going to be able to target this location to insert our React content much the same way you might use CSS to target an id for styling of your choice.

Any react content will be rendered within the div tags with the id of app. In the meantime we’ll leave some text saying that “React has not rendered yet” If we see this when we preview our page it means that somewhere we missed rendering react.

Now, let’s go ahead and create a script tag within our body where we will create with react for the first time. The syntax we are going to need for our script tag is to add an attribute of “type”. This specifies the media type of the script. Above in our head we used an src attribute that pointed to the external script files for the React library, ReactDOM library and Babel library.

<body>
  <div id="app">React has not rendered yet</div>
  <script type="text/babel">
  </script>
</body>

The type of script that we are using we’ll wrap in quotes and set it to text/babel.

We’ll need this ability to use babel right away as we work with JSX. First, we are going to render React to the DOM. We will use the ReactDOM.render() method to do this. This will be a method, and remember a method is just a function attached to an object. This method will take two arguments.

<body>
  <div id="app">React has not rendered yet</div>
  <script type="text/babel">
    ReactDOM.render(React What, React Where);
  </script>
</body>

The first argument is the “what” of react. The second argument is the “where” of the location you want it to be placed in the DOM Let’s start by calling our ReactDOM.render() method. Our first argument is going to be our JSX.

<body>
  <div id="app">React has not rendered yet</div>
  <script type="text/babel">
    ReactDOM.render(
      <h1>Hello World</h1>, 
      React Where
    );
  </script>
</body>

The official react docs state,

“This funny tag syntax is neither a string nor HTML. It is called JSX, and it is a syntax extension to JavaScript. We recommend using it with React to describe what the UI should look like. JSX may remind you of a template language, but it comes with the full power of JavaScript. JSX produces React 'elements'.”

Often times, JSX freaks people out who have been developers for a while because it looks like HTML. At a very early age developers are taught separation of concerns. HTML has its place, CSS has its place and JavaScript has it’s place. JSX seems to blur the lines. You are using what looks like HTML but as Facebook says comes with the full power of JavaScript.

This can freak out veterans so many react tutorials start without JSX which can be quite complex. We won’t do that. Because this course is directed towards those who are very young in their careers you may not bring those red flags when you see this syntax.

And JSX is just really intuitive. You can probably quite easily read this code and see that this is going to be the largest header tag displaying the text “Hello World”. No mystery and pretty straightforward.

Now, let’s look at what our second argument would be.

<body>
  <div id="app>React has not rendered yet</div>
  <script type="text/babel">
    ReactDOM.render(
      <h1>Hello World</h1>, 
      Document.getElementById("app)
    );
  </script>
</body>

This is where we want our react content rendered to the dom. You’ve probably done this quite a few times in the past. We’ll just type in document.getElementById(). And we’ll pass into the argument of the id of app. And that is it. We will now target the div with the id of app to insert our react content.

We want to make sure our content is saved. Go ahead and open this up in the browser and you should see “Hello World”. As you can probably guess, using React is not the quickest or best way to create a Hello World app. We aren’t quite seeing the benefits of it yet. But now, we know that everything is working.

Go ahead and open up the console and look at the “elements”. You can do that on a mac with command + shift + j or on a On Windows and Linux: Ctrl + Shift + J

If you click on the head tag we can see our script libraries we included. Then we can go down to body of our document. Let’s click on our div with the id of “app”. And when we do we see our <h1> tag with the content “Hello World”.

So let’s do a quick recap. In our head tag we grabbed the script tags for React, React DOM and babel. These are the tools our browser needs in its meta data to read our react code and JSX in specific.

We then located the position within the DOM that we wanted to insert our React by creating an element div with the id of “app”.

Next, we created a script tag to input our react code. We used the ReactDOM.render() method that takes two arguments. The “what” of the react content, in this case our JSX, and the second argument is the “where” that you want to insert the React content into the DOM. In this case it is the location with the id of “app”.

You can access the final code to this lesson in this GitHub repo.


Want to learn React? Check out Code 401: Advanced Software Development in Full-Stack JavaScript »


Enjoy this content? Follow Rob on Medium »

Next PostPrevious Post

About the Author