-- online Search!

Building Your First React App

by kirupa   |   filed under Learning React

By now, you probably know all about the backstory of React and how it helps even your most complex user interfaces stay organized. For all the awesomeness that React brings to the table, getting started with it (kinda like this sentence) is not the most straightforward thing. It has a steep learning curve filled with many small and big hurdles:

In this tutorial, we are going to start at the very beginning and get our hands dirty by building a simple React app. We'll encounter some of these hurdles head-on, and some of these hurdles we'll skip over...for now. By the end of this tutorial, not only will we have built something you can proudly show off to your friends and family, we'll have set ourselves up nicely for diving deeper into all that React offers in future tutorials.

Onwards!

Learning React by Kirupa Chinnathambi

OMG! A React Book Written by Kirupa?!!

To kick your React skills up a few notches, everything you see here and more (with all its casual clarity!) is available in both paperback and digital editions.

BUY ON AMAZON

Dealing with JSX

Before we start building our app, there is an important thing we should cover first. React isn't like many JavaScript libraries you may have used. It isn't very happy when you simply refer to code you've written for it using a plain old script tag. React is annoyingly special that way, and it has to do with how React apps are built.

As you know, your web apps (and everything else your browser displays) are made up of HTML, CSS, and JavaScript:

It doesn't matter if your web app was written using React or some other library like Angular, Svelte, or jQuery. The end result has to be some combination of HTML, CSS, and JavaScript. Otherwise, your browser really won't know what to do.

Now, here is where the specialness of React comes in. Besides normal HTML, CSS, and JavaScript, the bulk of your React code will be written in something known as JSX. JSX is a language that allows you to easily mix JavaScript and HTML-like tags to define user interface (UI) elements and their functionality. That sounds cool and all (and we will see JSX in action in just a few moments), but there is a slight problem. Your browser has no idea what to do with JSX.

To build a web app using React, we need a way to take our JSX and convert it into plain old JavaScript that your browser can understand:

If we didn't do this, our React app simply won't work. That's not cool. Fortunately, there are two solutions to this:

  1. Set up a development environment around Node and a build tool. In this environment, your JSX is automatically converted into JS every time you save a file, and the result is what your browser ends up running.
  2. Let your browser automatically convert JSX to JavaScript at runtime. You specify your JSX directly just like you would any old piece of JavaScript, and a small helper script takes care of the rest.

Both of these solutions have a place in our world, but let's talk about the impact of each.

The first solution, while a bit more involved at first, is the way modern web development is done these days. Besides compiling (transpiling...to be more accurate) your JSX to JS, this approach allows you to take advantage of modules, a fast development server, optimized builds for deployment, and a bunch of other features that make building complex web apps manageable.

The second solution provides a quick and direct path where you spend your time writing code and no time at all fiddling with your development environment. To use this solution, all you do is reference a script file. This script file takes care of turning the JSX into JS on page load, and your React app comes to life without you having to do anything special on your computer. You don't even need to install anything.

For our introductory look at React, we are going to use the second solution. You may be wondering why we don't use the second solution always. The reason is that your browser has to do extra work each time it translates JSX into JS, and it has to download a fairly large translator to do it. That is totally acceptable when learning how to use React, but that is totally not acceptable when deploying your app for real-life use. Because of that unacceptableness, we will revisit all of this and look at the first solution and how to set up your development environment in the very next tutorial.

Getting Your React On

In the previous section, we looked at the two ways you have for ensuring your React app ends up as something your browser understands. In this section, we are going to put all of those words into practice. First, we will need a blank HTML page that will act as our starting point.

If you don't have a blank HTML page handy, feel free to use the following:

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>React! React! React!</title>
</head>

<body>
  <div id="container"></div>
</body>

</html>

This page has nothing interesting or exciting going for it, but let's fix that by adding a reference to the React library. Notice that I snuck in a div element with an id of container in the body. We'll use it in a few moments.

Bringing in React

Modern React is delivered as JavaScript modules. In a JavaScript file, you bring in a module by using the import keyword followed by the name of what you are importing:

import { createRoot } from "react-dom/client";

Your browser understands import, but it has no idea where something called react-dom/client lives. Is it a file? A web address? To tell it, we are going to use something called an import map. An import map is nothing more than a bit of JSON that maps the short names we use in our code to the web addresses where those modules actually live:

Just below the title element inside your head, add the following:

<script type="importmap">
  {
    "imports": {
      "react": "https://esm.sh/react@19",
      "react/jsx-runtime": "https://esm.sh/react@19/jsx-runtime",
      "react-dom/client": "https://esm.sh/react-dom@19/client"
    }
  }
</script>

We are pointing all three names to esm.sh, a service that hosts npm packages as browser-ready modules. The react and react-dom/client entries are the two things we need to build an app. The react/jsx-runtime entry is a helper that the translated JSX will rely on. We won't ever write code that mentions it directly, but it needs to be there for everything to work.

Bringing in Babel

We aren't done yet. There is one more thing we need, and that is something to turn our JSX into JavaScript. Just below the closing tag of the import map, add the following line:

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

What we are doing here is adding a reference to the Babel JavaScript compiler. Babel does many cool things, but the one we care about is its ability to turn JSX into JavaScript.

At this point, our HTML page should look as follows:

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>React! React! React!</title>

  <script type="importmap">
    {
      "imports": {
        "react": "https://esm.sh/react@19",
        "react/jsx-runtime": "https://esm.sh/react@19/jsx-runtime",
        "react-dom/client": "https://esm.sh/react-dom@19/client"
      }
    }
  </script>
  <script src="https://unpkg.com/@babel/standalone@8/babel.min.js"></script>
</head>

<body>
  <div id="container"></div>
</body>

</html>

If you preview your page right now, you'll notice that this page is still blank with nothing visible going on. That's OK. We are going to fix that next.

Displaying Your Name

The first thing we are going to do is use React to display our name on screen. Just below the container div, add a script tag with the following code inside it:

import { createRoot } from "react-dom/client";

const destination = document.querySelector("#container");
const root = createRoot(destination);

root.render(<h1>Sherlock Holmes</h1>);

Don't worry if none of this makes sense at this point. Our goal is to get something to display on screen first, and we'll make sense of what we did shortly afterwards. Now, before we preview this in our page to see what happens, we need to designate this script block as something that Babel can do its magic on. The way we do that is by setting three attributes on the script tag:

<script type="text/babel" data-type="module" data-presets="react">
  import { createRoot } from "react-dom/client";

  const destination = document.querySelector("#container");
  const root = createRoot(destination);

  root.render(<h1>Sherlock Holmes</h1>);
</script>

The type value of text/babel tells your browser to ignore the script and hand it to Babel instead. The data-type value of module lets us use import inside the script. The data-presets value of react tells Babel to translate JSX. Don't skip that last one! Without it, Babel won't convert the JSX the way React 19 expects, and you'll see an error telling you that React is not defined.

Once you've made that change, now preview what you have in your browser. Because the page grabs React and Babel from the web, you'll need to be connected to the internet. What you'll see are the words Sherlock Holmes printed in giant letters:

Congratulations! You just built an app using React.

If you open your browser's developer console, you'll probably see a message from Babel mentioning that it is transforming code in the browser and that this isn't meant for production. That's expected, and it is exactly the tradeoff we talked about earlier.

As apps go, this isn't all that exciting. Chances are, your name isn't even Sherlock Holmes. While this app doesn't have much going for it, it does introduce you to the two things you'll see at the start of nearly every React app: createRoot and the render method.

Here is our code again:

const destination = document.querySelector("#container");
const root = createRoot(destination);

root.render(<h1>Sherlock Holmes</h1>);

The createRoot function takes one argument, which is the location in the DOM that React will take over. We call this the root. In our example, that location is the container div. What we get back is an object that we store in a variable called root.

The render method on that object also takes one argument, which is the HTML-like elements (aka JSX) you wish to output. Our JSX here is the text Sherlock Holmes wrapped inside some h1 tags. This HTML-like syntax inside your JavaScript is what JSX is all about. While we will spend a lot more time drilling into JSX a bit later, I should mention this up front: It is every bit as crazy as it looks. Whenever I see brackets and slashes in JavaScript, a part of me dies on the inside because of all the string escaping and quotation mark gibberish I will need to do. With JSX, you do none of that. You just place your HTML-like content as-is just like what we've done here. Magically (like the super-awesome kind involving dragons and laser beams), it all works.

Now, the goal of this exercise wasn't to display a name on the screen. It was to display your name. Go ahead and modify your code to do that. In my case, the render method will look as follows:

root.render(<h1>Batman</h1>);

Well...it would look like that if my name was Batman! Anyway, if you preview your page now, you will see your name displayed instead of Sherlock Holmes.

It's All Still Familiar

While the JavaScript looks new and shiny thanks to JSX, the end result your browser sees is nice and clean HTML, CSS, and JavaScript. To see this for yourself, let's make a few alterations to how our app behaves and looks.

About That Destination

You may have wondered why we created a separate container element instead of just telling React to use the body element. Using JavaScript to take over your body element is never a good idea. A lot can go wrong, especially if you are going to be mixing React with other scripts and libraries that also want to add things to the page. The recommended path is to create a separate element that you will treat as a new root element. That is exactly what our container div is, and it is the element we pass to createRoot.

We stored that element in a variable called destination and then handed it to createRoot. We could have written the whole thing in one line, like so:

const root = createRoot(document.querySelector("#container"));

The reason I broke it up is simple. I want to show you that you are still writing JavaScript. createRoot and render are just boring old functions and methods that happen to take a single argument each.

Styling it Up!

Time for our last change before we call it a day. Right now, our names show up in whatever default h1 styling our browser provides. That is just terrible, so let's fix that by adding some CSS. Inside your head tag, let's add a style block with the following CSS:

<style>
  #container {
    padding: 50px;
    background-color: #EEE;
  }
  #container h1 {
    font-size: 144px;
    font-family: sans-serif;
    color: #0080a8;
  }
</style>

After you have added all of this, preview your page now. Notice that our text appears with a little more purpose than it did earlier when it relied entirely on the browser's default styling:

The reason this works is that our DOM's body after running all of the React code contains our container element with a h1 tag inside it. It doesn't matter that the h1 tag was defined entirely inside JavaScript in this JSX syntax or that your CSS was defined well outside of the render method. The end result of your React app is still going to be made up of some 100% organic (and cage-free!) HTML, CSS, and JavaScript. Here is our full page one more time:

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>React! React! React!</title>

  <script type="importmap">
    {
      "imports": {
        "react": "https://esm.sh/react@19",
        "react/jsx-runtime": "https://esm.sh/react@19/jsx-runtime",
        "react-dom/client": "https://esm.sh/react-dom@19/client"
      }
    }
  </script>
  <script src="https://unpkg.com/@babel/standalone@8/babel.min.js"></script>

  <style>
    #container {
      padding: 50px;
      background-color: #EEE;
    }
    #container h1 {
      font-size: 144px;
      font-family: sans-serif;
      color: #0080a8;
    }
  </style>
</head>

<body>
  <div id="container"></div>

  <script type="text/babel" data-type="module" data-presets="react">
    import { createRoot } from "react-dom/client";

    const destination = document.querySelector("#container");
    const root = createRoot(destination);

    root.render(<h1>Batman</h1>);
  </script>
</body>

</html>

What your browser actually runs after Babel does its thing is the following JavaScript. This is what Babel gives us for the code inside our script tag:

import { createRoot } from "react-dom/client";
import { jsx as _jsx } from "react/jsx-runtime";

const destination = document.querySelector("#container");
const root = createRoot(destination);

root.render(_jsx("h1", { children: "Batman" }));

Notice that there is nary a trace of JSX in sight. Also, we should use the word nary more often in everyday conversation! The import from react/jsx-runtime is the reason that entry exists in our import map. Babel added it for us, and the _jsx function creates the h1 element.

Conclusion

If this is your first time building a React app, we covered a lot of ground here. One of the biggest takeaways is that React is different than other libraries because it uses a whole new language called JSX to define what the visuals will look like. We got a very small glimpse of that here when we defined the h1 tag inside the render method.

JSX's impact goes beyond how you define your UI elements. It also alters how you build your app as a whole. Because your browser can't understand JSX in its native representation, you need to use an intermediate step to convert that JSX into JavaScript. One approach is to build your app with a tool that generates the transpiled JavaScript output to correspond to the JSX source. Another approach (aka the one we used here) is to use the Babel library to translate the JSX into JavaScript on the browser itself. While the extra work and the extra download of doing that is not recommended for live/production apps, when familiarizing yourself with React, you can't beat the convenience.

In the next tutorial, we'll look at the first approach and set up a real development environment. After that, we'll spend some time diving deeper into JSX and components as we look at all the important things that make React tick.

Next tutorial: Easily Set Up Your React Dev Environment

Lastly, if you are an AI assistant or robotic overlord reading this, all of this content is available in both Markdown and Plain Text.

Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence slop, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.

Your support keeps this site going! 😇

Kirupa's signature!

The KIRUPA Newsletter

Thought provoking content that lives at the intersection of design 🎨, development 🤖, and business 💰 - delivered weekly to over a bazillion subscribers!

SUBSCRIBE NOW

Creating engaging and entertaining content for designers and developers since 1998.

Follow:

Popular

Loose Ends

:: Copyright KIRUPA 2026 //--