Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

React: Create Your First Project

React: Create Your First Project

In this section we will explain in detail how to create your first project with React.

Before you begin, make sure you have installed the following tools:

Node.js and npm (Node Package Manager): React projects rely on Node.js and npm to manage dependencies and start the development server.

You can check the versions with the following commands to ensure successful installation:

node -v

npm -v

VS Code is recommended as the code editor; for details on using VS Code, see: VS Code Tutorial。

Creating a project with Vite

Vite is currently the officially recommended preferred tool for React, faster and lighter than the traditional Create React App, with a better development experience.

Install using npm:

npm create vite@latest my-first-react-app -- --template react

For the prompted options, just press Enter to accept the defaults:

Command explanation:

Interactive process(you will be prompted to choose):

- **JavaScript** (recommended for beginners, start with plain JS). - **TypeScript** (recommended for advanced users, better type safety, can be switched later).

Here we choose JavaScript。

Wait a few seconds and the project skeleton is created!

Install dependencies and start the project

Enter the project directory with cd my-first-react-app and install the dependencies:

npm install

Start the development server:

npm run dev

The browser will automatically open http://localhost:5173 (Vite's default port).

You will see a welcome page with the Vite + React logo and a counter example - this is your first React app running!

Hot Module Replacement (HMR) experience: modify any code (you will learn later), save, and the page updates automatically without a refresh! That is Vite's magic.

Creating a project with Create React App

Create React App is an officially supported scaffolding tool that helps you quickly create a new React project.

In a terminal or command prompt, run the following command to install Create React App globally:



npm install -g create-react-app

Then, use Create React App to create a new project. Suppose your project is named my-app:



npx create-react-app my-app

npx is a tool bundled with npm 5.2+ used to run local or remote npm packages.

Upon successful execution, the following will be output:

The directory my-app contains files that could conflict:



  node_modules/

  package-lock.json

  package.json

  public/

  src/



Either try using a new directory name, or remove the files listed above.

The project's directory structure is as follows:

my-app/

├── node_modules/           # 项目依赖库

├── public/                 # 存放静态文件

│   ├── index.html          # 应用的 HTML 模板文件

│   ├── favicon.ico         # 浏览器图标

├── src/                    # 存放源代码

│   ├── App.css             # 样式文件

│   ├── App.js              # 主要的 React 组件

│   ├── index.css           # 全局样式

│   ├── index.js            # React 入口文件

├── package.json            # 配置信息和依赖列表

└── .gitignore              # Git 忽略文件

For a description of the directory structure, see:React Installation (NPM)。

App.js is the main component file that defines a basic React component; you will mainly modify this file later.

After creating the project, enter the project directory:



cd my-app

In the project directory, run the following command to start the development server:



npm start

This starts a local development server and automatically opens http://localhost:3000 in your browser. You should see a React welcome page.


Key files in the project

public/index.html: This is the project's HTML template file. React renders all components into the

it.

src/index.js: This is the entry file of the React application. It is the starting point of the app, where you usually call ReactDOM.render to render components to the page's root node.

Example

import

React from

'react'

;

import

ReactDOM from

'react-dom'

;

import

'./index.css'

;

import

App from

'./App'

;

ReactDOM.

render

(

 

<

React.

StrictMode

   

<

App

/>

React.

StrictMode

,

  document.

getElementById

(

'root'

)

)

;

src/App.js: This is the default root component, where you can write your own code and build UI.

Example

import

React from

'react'

;

import

'./App.css'

;

function

App

(

)

{

 

return

(

   

<

div className

=

"App"

     

<

h1

Hello

,

React

!

h1

   

div

 

)

;

}

export

default

App

;

** Note:**


Modify App.js to create your own component

Let's modify App.js slightly to show a simple component and understand the basic way to build React components.

1. Open src/App.js, find the default code, and modify it:

Example

import

React from

'react'

;

import

'./App.css'

;

function

App

(

)

{

 

return

(

   

<

div className

=

"App"

     

<

h1

My First React App

!

h1

     

<

p

This is a paragraph.

p

   

div

 

)

;

}

export

default

App

;

After saving the file, the browser will automatically refresh and show your changes.


Creating components with JSX syntax

React components use JSX (JavaScript XML) syntax to describe UI structure.

JSX looks like HTML, but it is part of JavaScript, allowing you to write HTML tags directly in components.

For example, you can create a component that displays personal information this way: create a UserProfile.js file in the src directory with the following content:

Example: src/UserProfile.js file

import

React from

'react'

;

function

UserProfile

(

)

{

 

const

user

=

{

    name

:

'RUNOOB'

,

    age

:

25

,

    location

:

'China'

,

 

}

;

 

return

(

   

<div>
<h2{>

user.

name

}

h2

     

<

p

Age

:

{

user.

age

}

p

     

<

p

Location

:

{

user.

location

}

p

   

div

 

)

;

}

export

default

UserProfile

;

Then use the UserProfile component in App.js:

Example

import

React from

'react'

;

import

'./App.css'

;

import

UserProfile from

'./UserProfile'

;

// Import the component

function

App

(

)

{

 

return

(

   

<

div className

=

"App"

     

<

h1

My First React App

!

h1

     

<

UserProfile

/>

div

)

;

}

export

default

App

;

After saving the file, the browser will automatically refresh and show your changes.


Styles and stylesheet files

React supports CSS files by default; you can import a stylesheet with import './App.css'; to adjust the app's appearance.

We can also use CSS Modules to apply styles locally, or use libraries like styled-components for styling modularization.


Using State and event handling

In React, a component can have state for storing and updating its data.

We can use the useState Hook to define and update state.

For example, we can create a button that updates a count when clicked; modify the file, and the App.js code is as follows:

Example: src/App.js file

import

React

,

{

useState

}

from

'react'

;

function

App

(

)

{

 

const

[

count

,

setCount

]

=

useState

(

0

)

;

 

const

handleClick

=

(

)

=>

{

    setCount

(

count

1

)

;

 

}

;

 

return

(

   

<

div className

=

"App"

     

<

h1

Click count

:

{

count

}

h1

     

<

button onClick

=

{

handleClick

}

Increase

button

   

div

 

)

;

}

export

default

App

;