React - getting started

First steps in React: the three parts of the library – React, React-DOM and Babel – and a first small app that renders JSX into the page.

React is a library that helps produce the user interface on the client side.

React was influenced by AngularJS and XHP (a PHP library for managing HTML components).

Reading the documentation, you can see that React defines itself as a library, a JavaScript library for building user interfaces.

The library was first used on Facebook’s home page in 2011 and later, in 2012, on Instagram as well. The library was released as open source in May 2013.

The three parts of React

React is made up of three parts:

  1. The React library
  2. react-dom
  3. babel

1. The React library

The React library consists of a lot of material written in JS. When we build a first basic app for a taste and a demonstration, we will need to load the library. We load it like this:

HTML
<script src="https://unpkg.com/react@16/umd/react.production.min.js"></script>

2. React-DOM

Another part of the library, which holds a virtual DOM, a copy of the DOM. React works in such a way that at any given moment it performs updates on its virtual DOM, and in one go outputs it through rendering and updates the real DOM – mediating between our work and the browser. We will need to load it too for the initial demonstration. We load it like this:

HTML
<script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>

3. Babel

The third and last part is Babel. This is a library, also written in JS, that transpiles the JSX code and translates the code into HTML tags with content.

We load Babel like this:

HTML
<script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>

A first small app

Usually you don’t work this way, by loading the files above; instead you set up a project that takes care of loading everything required to start working. But in order to understand things, it is worth first creating a small app in which we load the three scripts above, and start writing the React code in a script tag.

To insert a React area into our content page, we create an element with an id of our choice, and render the React code we write into it.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
    <script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
    <script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
</head>
<body>
    <div class="root"></div>    
</body>
</html>

Writing JSX

When we want to write a React script, we specify in the script’s type that the code is code that needs to be interpreted into tags and text, using type="text/babel":

JSX
<script type="text/babel">
    let containerElement=document.getElementById("root");        

    let element = <div>
    <h1>Hello React</h1>
    <h2>React is easy </h2>           
    </div>;

    ReactDOM.render(element, containerElement);
 </script>

As you can see, inside the script we wrote code that is neither JS nor HTML, a kind of mixture: there are HTML tags in the script code, but they are not enclosed as a string. This code is JSX code.

And so that the browser recognizes it, we loaded Babel at the start of the app, which interprets the code above into regular HTML tags.

Search and commands