Step 2: Create a React application

When people talk about complexity in React development, often they’re referring not to React itself, but in the build system and development environment.

They have a point — as React developers, we tend to build pretty complex dev environments and build systems. We suffer this complexity because it makes React development quicker and more enjoyable.

However, that complexity is overwhelming for those just getting started, and worse, it gets in the way of learning what’s really important: React itself.

Thankfully, there is a have-your-cake-and-eat-it-too solution. It’s called create-react-app.

create-react-app is a command-line tool, owned by Facebook and maintained by talented developers both at Facebook and in the community. It’s amazing. It abstracts away all the complexity and difficulty of implementing Webpack, Babel, a dev server, a production build process, and a thousand other tedious-but-critical things.

It’s zero-configuration by default, and doesn’t clutter up your project directory with a bunch of files you don’t understand.

Note: Eventually, you will want to spend the time learning how this works internally. There’s a lot of neat stuff you can do with Webpack, for example. Our top priority at the moment, though, is getting you excited about building with React, so we can defer this stuff for later.

Yarn comes with a built-in helper for using create-react-app:

yarn create react-app [your-app-name]

I’d like my application to be named cats-n-stuff, so I’ll run that:

The actual terminal output is a lot, so this screenshot cuts out a bunch of stuff in the middle.

If you get an error starting with “A template was not provided”, it’s likely because you have an earlier incompatible version of create-react-app installed. Check out this StackOverflow answer.



If you’re using NPM instead of yarn, you can run “npx create-react-app cats-n-stuff” to achieve the same result.

This command does quite a few things:

Downloads and installs create-react-app

Creates a new project folder, cats-n-stuff (or whatever you name your app)

(or whatever you name your app) Scaffolds out a new project, including everything you need to compile a production-ready app, as well as run a development server

Installs dependencies

You’ll notice the output towards the end gives you a set of commands.

Let’s start by following its instructions:

We should see the following output:

A browser window should also have opened, showing the following:

Let’s open our new project in our code editor. I’m using VS Code. You should see the following files:

Let’s go through them:

node_modules

This directory holds all of our project dependencies, like react and react-dom. It also includes all the dependencies needed to run our build system and development server. Sometimes, folks are alarmed by the size of this directory (~200mb), but it’s important to remember that it includes a whole bunch of dev tooling. Very little code is actually shipped to the user.

public

This directory holds static assets, stuff like our favicon and search-engine preferences. It also houses our project’s only HTML file. Because React is all-javascript, you only really need the HTML file to update the <head> stuff, like title and meta tags. This can always be modified with React as well, but for now it can be simplest to add your own <title> to this file.

src

This directory holds the source code we’ll write to build our application. By default, create-react-app comes with one component — App — and an index.js file that mounts it. It also includes some CSS files — it’s conventional in the React community to colocate styles and code, so that each component has its own stylesheet.

For now, you can ignore the final two files, serviceWorker.js and setupTests.js. We don’t need to fuss with them yet.

Root directory

Finally, the root directory of the project holds stuff related to dependencies (package.json and yarn.lock), as well as a comprehensive documentation (README.md) for create-react-app.