Behind the Scenes of Creating a React Project and its Repository Structure

Behind the Scenes of Creating a React Project and its Repository Structure

Table of contents

Are you curious about how a React project comes to life? We're about to take you on a journey into the inner workings of creating a React project. Starting with the basic command to set things in motion, we'll explore the various folders and files that form your project's home.

First, let's create a React project. While there are several ways to do this, we'll use create-react-app for its setup. However, if you're interested in alternative methods, you can learn more by clicking here. No matter which method you use the basic working remains the same.

Run the command npx create-react-app your_app_name.This will create a react project in your directory. Once you open the project directory you can see many files and folders inside it. They are listed below.

your-react-app/
  ├── node_modules/         # Dependencies installed by npm/yarn
  ├── public/               # Static assets
  │     ├── index.html      # The main HTML file
  │     ├── favicon.ico     # Favicon icon
  │     └── manifest.json   # Web app manifest
  ├── src/                  # Source code for your React app
  │     ├── components/     # React components(I have added this folder)
  │     ├── pages/          # Contains the pages(I have added this folder)
  │     ├── App.js          # Main React component
  │     ├── index.js        # Entry point for the app
  ├── package.json          # Project dependencies and scripts
  ├── package-lock.json     # Includes a complete list of all dependencies and sub-dependencies required by your project
  ├── .gitignore            # Git ignore file
  ├── .env                  # Environment variables (optional)
  └── README.md             # Project documentation

Let's go through them one by one:

  1. node_modules/:- The node_modules folder contains every installed dependency for your project. Using these dependencies our react project runs. In most cases, you should not commit this folder to your version-controlled repository as you install them using npm i command.

  2. public/: This directory holds static assets that are accessible by the web server. The most important file in this folder is index.html, which serves as the entry point for your React application.

    Within the index.html you can see a div with id set as "root". Whenever something needs to be rendered on the screen the required code and assets are loaded into this div we will see this in some time.

  3. src/: This is where your application's source code resides.

  • components/: This is a common location to organize your React components. You can create and store your custom components here and import and use them in your pages. This keeps your code well-organised and readable.

  • pages/: This is the folder where you store the web pages that you want to render like Homepage, About Us, Contact Us etc. You can import the components which you have written in the components folder and use them here to create your web page.

  • App.js: This is the main React component that serves as the root of your application. You can customize this based on your needs.

  • index.js: The entry point for your React app, which initializes the rendering of your application. The main code within it looks somewhat like this

      const root = ReactDOM.createRoot(document.getElementById('root'));
      root.render(
        <React.StrictMode>
          <App />
        </React.StrictMode>
      );
    
  • As you can see we are selecting an element with id root in the first line and then rendering our App component into it.

  1. package.json: This file contains information about your project, its dependencies, and scripts to run various development and build tasks.

  2. package-lock.json: This file is a file generated by npm and is used to provide a detailed, version-specific representation of a project's dependencies. This is to ensure that when the project is installed in another machine there is no bug related to dependency installed or it's version because there are cases when code might run on one version and not on other.

  3. README.md: Project documentation where you can provide information about your app, instructions for development, and other relevant details.

  4. .gitignore: A configuration file for Git that specifies which files and directories should be ignored when you commit changes to a version control system. This usually contains files such as node_modules or files which might contain sensitive information like API keys, user details etc.

References