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

React Installation (Vite)

React Installation (Vite)

Vite is a modern frontend build tool designed to provide a fast development experience by leveraging native ES module support in modern browsers.

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

Vite is based on native ES Modules, supports second-level hot module replacement (HMR), and startup usually takes only a few hundred milliseconds.

For details about Vite, see: Vite Tutorial。

Why choose Vite + React?


Create Your First Vite + React Project

Open a terminal (Windows: PowerShell/Command Prompt; macOS/Linux: Terminal).

Install using npm:

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

Recommended command (using pnpm, the fastest and most disk-efficient):



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

Other package manager commands:

Install using yarn:

yarn create vite my-first-react-app --template react

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.

Project Structure Explained

Open the project folder (VS Code is recommended: use the `code .` command to open it in one step).

cd my-first-react-app

code .

A typical structure is as follows:

my-first-react-app/

├── public/              # 静态资源(直接复制到构建输出)

│   └── vite.svg         # Vite Logo

├── src/                 # 源码主目录(重点!)

│   ├── assets/          # 图片等资源

│   │   └── react.svg

│   ├── App.jsx          # 主组件(应用入口)

│   ├── main.jsx         # 入口文件(挂载根组件)

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

│   └── vite-env.d.ts    # TypeScript 声明(JS 项目可忽略)

├── .gitignore

├── index.html           # HTML 入口模板

├── package.json         # 依赖和脚本

├── vite.config.js       # Vite 配置文件(目前为空,可自定义)

└── README.md

Key file explanations:

Modify the Code: Your First React Component

Open src/App.jsx, replace all content with the following code (we start with a simple greeting):

Example

import

{

useState

}

from

'react'

import

reactLogo from

'./assets/react.svg'

import

viteLogo from

'/vite.svg'

import

'./App.css'

function

App

(

)

{

 

const

[

count

,

setCount

]

=

useState

(

0

)

 

return

(

   

<

div className

=

"App"

     

<

div

<

a href

=

"https://vitejs.dev"

target

=

"_blank"

         

<

img src

=

{

viteLogo

}

className

=

"logo"

alt

=

"Vite logo"

/>

       

a

       

<

a href

=

"https://react.dev"

target

=

"_blank"

         

<

img src

=

{

reactLogo

}

className

=

"logo react"

alt

=

"React logo"

/>

       

a

     

div

     

<

h1

Hello, React

Vite!

h1

     

<

div className

=

"card"

       

<

button onClick

=

{

(

)

=>

setCount

(

(

count

)

=>

count

1

)

}

          Current count:

{

count

}

       

button

     

div

     

<p Editing>
<code src/>

App.

jsx

code

and save to experience hot reloading!

     

p

   

div

 

)

}

export

default

App

After saving, the page updates automatically! You have written a complete function component:

Small Exercises:

  1. Change the title to your name:

    你好,[你的名字]!

  2. Add a new button that shows subtraction counting.

Understanding the Entry File: src/main.jsx

Open src/main.jsx:

Example

import

React from

'react'

import

ReactDOM from

'react-dom/client'

import

App from

'./App.jsx'

import

'./index.css'

ReactDOM.

createRoot

(

document.

getElementById

(

'root'

)

)

.

render

(

 

<

React.

StrictMode

   

<

App

/>

React.

StrictMode

)

Explanation:

Adding New Components (Componentization Practice)

in src/ Create a new file under components/HelloWorld.jsx(create the folder manually first):

// src/components/HelloWorld.jsx

function HelloWorld({ name }) {

  return 

欢迎,{name || '陌生人'}!

} export default HelloWorld

in App.jsx Import and use it in:

import HelloWorld from './components/HelloWorld.jsx'



// Add to return:



Save and refresh, and you will see the new component rendered. This is the Componentization charm: reusable and composable!

Styles and Asset Handling

Building for Production

After development is complete, build the static files for deployment:

npm run build

Output is in the dist/ folder: pure HTML/CSS/JS, which can be uploaded to Vercel, Netlify, etc.