React has gained a significant heights in popularity over the past few years, establishing itself as one of the leading JavaScript libraries for building user interfaces. React simplifies the process of building UI components, while Create React App streamlines the project setup, enabling developers to create React applications efficiently.
Starting with your first React application may seem challenging at first, but fear not! In this tutorial, we will guide you through each step in detail, ensuring a smooth journey into the world of React. By the end of this blog, you’ll have the knowledge and confidence to create your very first React application.
Prerequisites:
Before diving into React, it’s beneficial to have a basic understanding of HTML, CSS, and JavaScript. Familiarity with JavaScript ES6 syntax and concepts such as functions, variables, and arrays will be helpful. Additionally, ensure that you have Node.js installed on your system to take advantage of its package manager, npm.
We recommend visiting our blog on Prerequisites for React, where we have provided detailed information on the necessary requirements.
In this tutorial we are going to discuss How you can create A react application using Create React App.
About Create React App
Create React App (CRA) is a command-line tool provided by the React team to create a new React application with a pre-configured development environment. It sets up a modern development environment with tools like Babel, Webpack, and ESLint, allowing developers to focus on building their React components and features without getting bogged down by the initial project setup.It is designed to make the setup process for React projects faster and more streamlined.
Steps to create Application with create-react-app
Create React App (CRA) is a command-line tool provided by the React team to create a new React application with a pre-configured development environment. It sets up a modern development environment with tools like Babel, Webpack, and ESLint, allowing developers to focus on building their React components and features without getting bogged down by the initial project setup.It is designed to make the setup process for React projects faster and more streamlined.
Open command promt and type the command
$ node --version
This way you will check if you have node.js installed on your system and its version. If not installed, you can quickly install latest version of node specific to your operating system from Node.js official website at https://nodejs.org/.
Run the following command to install Create React App globally
npm install -g create-react-app
Running the create-react-app command globally in the command prompt allows you to access the command from anywhere on your system. When you install Create React App globally, it sets up a global executable that can be executed from any directory in your command prompt or terminal.
Create a New React Project
Navigate to the directory where you want to create your React project. In the terminal or command prompt, run the following command to create a new React application
npx create-react-app my-app
We suggest using visual studio code or any other editor of your choice to run and execute this and further commands in the terminal as they provide extensions, plugins and tools specifically designed for React development. Replace “my-app” with the desired name of your project. This command will bootstrap a new React project with all the necessary files and dependencies.
Navigate to the Project Directory
Once the project is created, navigate to the project directory by running the following command:
cd my-app
Explore and Customize
When you create the Application with create-react-app, the project folder will contain necessary files and folders as shown. The Create React App project structure consists of the following directories:
- node_modules/: This directory contains all the project dependencies, such as packages that have been downloaded and installed using the Node.js Package Manager (NPM). These dependencies are essential for the proper functioning of the React application.
- public/: The public directory contains static assets of the web application, including the index.html file. This HTML file serves as the entry point for the React application and is responsible for rendering the React components into the browser.
- src/: The src directory is where you can find the JavaScript implementation of your React application. By default, you will find the App component implementation in the App.js file. This is the main component of your application and serves as the starting point for your React UI. Additionally, you can find the corresponding initial unit test case implementations in the App.test.js file. The index.js file contains the code that serves as the entry point of your React application and is responsible for rendering the App component into the DOM.
Start the Development Server
To run the React application, use the following command:
npm start
Here ‘start’ is a script to run the React application in development mode that you can find in the script section of package.json file.
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
This command will start the development server and open your application in your default browser at http://localhost:3000. Any changes you make to the source code will automatically trigger a refresh in the browser.
At this point, your React application is up and running. Open your code editor and navigate to the project directory. You’ll find the source code of your React application in the “src” directory. Start exploring the project structure and the initial files, such as “App.js” and “index.js”.
Feel free to customize the application according to your needs. You can modify the existing components, add new components, import and use external libraries, and more. Refer to the Create React App documentation for more information on available features and customization options.
Congratulations! You have successfully created a React application using Create React App.
Summery
Create React App is widely used and supported by the React community. It is a great starting point for beginners learning React or experienced developers looking for a quick and efficient way to bootstrap new React projects without getting overwhelmed by the initial setup process.