React Create App

To create a React app, the first step is to set up the development environment. As discussed in the React Environment Setup article, there are two ways to do this: either by using React CDNs or by setting up the React environment directly on our local machine. In this case, we prefer setting up the environment locally for better control and flexibility. To understand why we have chosen this option and to learn how to set up the development environment, please refer to the React Environment Setup guide.

 

Now, open your terminal, navigate to the folder where you want to create your React app and execute the following command.

 

npx create-react-app react-greeting-app

This command will create a new folder called "react-greeting-app" with all the necessary files and configurations to get started with your project. Your file structure will appear as shown below.

 

React create app folder structure

 

Before getting deep into these files and folders, let's start the development server. In the terminal, execute the following command. Alternatively, if you're using the Visual Studio Code editor, you can open your application folder and run the same command in the terminal.

 

npm start

This command will start the development server, and your React app will automatically open in your browser at "localhost:3000". If it doesn't open automatically, you can manually visit this address in your browser.

 

React start development server

 

If you wish to modify the output content, you will need to make changes to the App.js file. To do that, open the App.js file located in the src folder. It will contain the following code.

 

import logo from './logo.svg';
import './App.css';

function App() {
  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
          <p>
            Edit <code>src/App.js</code> and save to reload.
          </p>
          <a className="App-link" href="https://reactjs.org" target="_blank" rel="noopener noreferrer" > Learn React</a>
      </header>
    </div>
  );
}

export default App;

 Now, replace the content within the <div className="App"> with an <h1> element, as shown below.

 

import logo from './logo.svg';
import './App.css';

function App() {
  return (
    <div className="App">
      <h1>Hello, React!</h1>
    </div>
  );
}

export default App;

After making these changes, save the react application and verify it by accessing localhost:3000 in your browser. You should see the following result.

 

React Hello World App Example Result

 

Here's a concise overview of the important files and folders generated by the npx create-react-app react-greeting-app command:

 

1. node_modules: This folder contains various packages and dependencies required for your React application. It includes multiple folders and files, representing the installed libraries.

 

2. public/index.html: This HTML file acts as the entry point for your React app. It provides the basic structure of an HTML document and a <div> element with an id where your React app will be rendered. This is the only html file which serves the whole application. The rest of the html code is managed by the React Virtual DOM.

 

Index.html

 

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta name="description" content="Web site created using create-react-app" />
    <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
    <title>React App</title>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
  </body>
</html>

3. src/App.js: This JavaScript file represents the main component of your React app. It contains the code that defines the structure and behavior of your application. By default, it renders a simple greeting message.

 

App.js

 

import logo from './logo.svg';
import './App.css';

function App() {
  return (
    <div className="App">
      <h1>Hello, React!</h1>
    </div>
  );
}

export default App;

The function returns JSX. JSX is a syntax extension for JavaScript that allows you to write HTML-like code in your JavaScript files. The App.js file is a component that returns the JSX containing a div and h1 elements.

 

4. src/index.js: This JavaScript file serves as the entry point of your React app. Its primary responsibility is to render the root component, which is App.js, and inject it into the HTML file.

 

index.js

 

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);

Instead of rendering the App.js file, you can also write your own JavaScript function in the index.js file and render it by replacing the <App /> component with your own function, as shown in the following example.

 

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';

function Welcome() {
  return (
    <div className="App">
      <h1>Hello, React!</h1>
    </div>
  );
}

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <Welcome />
  </React.StrictMode>
);

5. package.json: This file contains metadata and configuration for your project, including dependencies, scripts, and more.

 

package.json

 

{
"name": "greeting-app",
"version": "0.1.0",
"private": true,
"dependencies": {
  "@testing-library/jest-dom": "^5.17.0",
  "@testing-library/react": "^13.4.0",
  "@testing-library/user-event": "^13.5.0",
  "react": "^18.2.0",
  "react-dom": "^18.2.0",
  "react-scripts": "5.0.1",
  "web-vitals": "^2.1.4"
},
"scripts": {
  "start": "react-scripts start",
  "build": "react-scripts build",
  "test": "react-scripts test",
  "eject": "react-scripts eject"
},
// ...
}

This is how we can create and make changes to the React app based on our requirements. In the upcoming chapters, we will learn about other react topics such as JSX, components, fragments, etc. in-detail with examples.