Discover Create React App (CRA)

Do you feel like you’re starting to master the basics of React? That’s great! Because in this part, we’re going to take things to the next level and buildĀ a complete app.

Understand the Importance of Automated Tools

You’ve learned how to use React and ReactDOM CDN links, and configure Babel in CodePen to create a React app quickly. However, this technique isĀ rarely used by professional developers.

Instead, they use automated tools to create a codebase containing essential tools that have already been preconfigured.Ā Some functionalities of these tools allow you to:

  • Manage different dependencies (libraries) used by an app.

  • Optimize code fetching in browsers.

  • Import CSS and images.

  • Manage different versions of JavaScript.

  • Make developing easier by refreshing the page when the code is modified.Ā 

Install and Launch CRA

Also created by teams at Facebook, Create React App is a tool that will help you to do everythingĀ  just mentioned. There are other tools available (Next, Gatsby, Parcel, etc.), but Create React App remains the standard, especially for new React users.

Create React App allows you to generate a skeleton code for your app. It includes someĀ preconfigured tools such as Webpack, Babel, and ESLint to guarantee the best development experience.Ā 

To begin, get into the folder where you want to create your project. For example, I’ve created aĀ  Ā workflowĀ  Ā folder that sits on the same level as Downloads, Documents, etc., which is where I file all my code projects.

To start the project:

npx create-react-app jungle-house

What doesĀ Ā npxĀ  mean in all of this?!

There's no need to go into too much detail as you’ll only use it this once, butĀ Ā npxĀ  is the command recommended by Create React App for setting up a project without having to install CRA globally or locally.

It’s loading... ā³āŒ›ļøā³āŒ›ļø

Yay! You now have your first project created with Create React App!

Explore the Files

Now that you’ve set up your project, it’s time to get in there and start exploring the created files. You can getĀ to the project withĀ Ā cd jungle-houseĀ  on your terminal.

From there, you can open your preferred text editor (I’m a fan of VS Code, personally šŸ˜Ž) to take a look. Whenever you’re ready, let’s go through the files together in this screencast:Ā Ā 

As you can see, you have three folders šŸ—‚ :

  • Ā node_modulesĀ : where all the dependencies for your code are installed. This folder can quickly become pretty large.Ā 

  • Ā publicĀ : whereĀ you’ll find your Ā index.htmlĀ  fileĀ  and others relating to the online listing of your site.

  • Ā srcĀ : where the action happens. The essential content of the files that you create and modify will be here.

Now let’s look at the most important files:

  • package.jsonĀ : located in your project root. Use this file to manage your dependencies (all of the tools you use to build your projects) and the scripts that you’ll run withĀ Ā yarnĀ , etc. If you look at its contents, you’ll see some dependencies that you recognize: React and React DOM.Ā 

    • Here you’ll findĀ Ā react-scriptsĀ , created by Facebook, which you can use to install Webpack, Babel, ESLint, etc. to make your life easier.Ā 

  • InĀ Ā /publicĀ , you’ll findĀ Ā index.htmlĀ .Ā It is the template for your application. There are lots of lines of code, but do you see Ā ```<div id="root"></div>```Ā  ? Just like in the chapters before, this is whereĀ you anchor the React app.Ā 

  • InĀ Ā /srcĀ , you’ll findĀ Ā index.jsĀ , which we’ll use to set up our React app.

  • Finally, inĀ  Ā /srcĀ , you’ll findĀ  Ā App.jsĀ ,Ā  which is our first React component.

I'm seeingĀ Ā dependenciesĀ  andĀ Ā devDependenciesĀ , but what’s the difference between the two?Ā 

dependenciesĀ  Ā are the dependencies you need for code to work, both locally and in production. However, you only needĀ devDependenciesĀ  in the development phase. Both are installed from your package manager (yarn), with the different options specified as follows:Ā  Ā yarn addĀ  forĀ Ā dependenciesĀ  Ā andĀ Ā yarn add --devĀ  forĀ Ā devDependenciesĀ .

There are two files that we won’t use directly, but which it’s a good idea to keep:

  • README.mdĀ , which you can use to generate an explanatory page if you put your code on GitHub, for example.

  • .gitignoreĀ , which specifies what should not be tracked with git (usually the largeĀ  Ā Ā node_modulesĀ  file).

Learn Commands

When you find your project root, you can runĀ Ā yarn startĀ , which will run your app in the development mode.

You’ll end up with something like this (but most likely with a different IP address):

The message reads, Compiled successfully! You can now view jungle-house in the browser. Then a local address and a network address, followed by a note.
Running Yarn start.

A tab should have opened in your browser with the URLĀ http://localhost:3000/. If not, open it yourself.Ā 

And ta-da! 🄳
You should see the glorious React logo spinning in your browser!

Organize Your Code

Let'sĀ modify the codebase so that it better resembles our project. There are several ways of organizing your code, and it’s important to think about how to do it. Here we’re going to separate files according to type: components/style/images, etc.Ā Ā 

We’ll start by creating aĀ /componentsĀ  folderĀ inĀ Ā /srcĀ , where we’ll put our components. Then dragĀ Ā App.jsĀ  into it and change the import path inĀ Ā index.jsĀ .

Ā Now let’s create theĀ Ā BannerĀ Ā from the previous chapter in a separate JavaScript file and call itĀ Ā banner.jsĀ .

function Banner() {
    return <h1>Jungle House</h1>
}

export default Banner

Ā Let's now now change the code inĀ  Ā App.jsĀ  by deleting the basic code and importingĀ the Ā BannerĀ .Ā 

import Banner from './Banner'
Ā 
function App() {
    return <Banner />
}
Ā 
export default App

This gives us:

Jungle House banner is visible at the address localhost:3000.
Banner imported.

Congratulations! You’ve reused your code from the last chapter!

The tool we're using to import our components is Webpack:

Webpack logo.
Import your components using Webpack.

Webpack makes importing a component easy with   import . This tool is essential for linking files together so that the browser can interpret them. And with Create React App, you can install this with no configuration required. 😁 

Give It a Go!

You can begin this exercise using the codebase that we created together with Create React App.

Your mission is toĀ import theĀ Ā CartĀ  component that you created in the last chapter into a separate file in theĀ Ā /componentsĀ  folder.

You’ll find the solution to this exercise in the course’s GitHub repository on theĀ P2C1-SolutionĀ branch.

Let’s Recap!

  • Developers use automated tools to make developing easier.Ā 

  • Create React App (CRA) is a toolbox created by Facebook that remains the standard for setting up a React project.

  • Projects created using CRA will always have:Ā 

    • AnĀ Ā index.htmlĀ  file, which is the template where a React app will live.Ā 

    • Ā package.jsonĀ , which lists dependencies and scripts.

    • Ā index.jsĀ  in which our React app is set up and joined to the HTML.

  • CRA runs with the help of a package manager (in this course,Ā Ā yarnĀ ).

  • Webpack allows for the simple importing of files between one another.Ā 

Did you enjoy creating your app with CRA?Ā Want to make things a bit more stylish? See you in the next chapter!Ā 

Ever considered an OpenClassrooms diploma?
  • Up to 100% of your training program funded
  • Flexible start date
  • Career-focused projects
  • Individual mentoring
Find the training program and funding option that suits you best