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?
- Lightning-fast startup: The dev server starts in seconds, and hot updates are nearly real-time.
- Modern features: Out-of-the-box support for JSX, TypeScript, CSS Modules, PostCSS, and more.
- Lightweight and flexible: Simple configuration files that are easy to customize.
- Production optimization: Built-in Rollup build with small output size and high performance.
- Official endorsement: The official React documentation now recommends Vite by default.
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:
create vite@latest: use the latest version of the Vite creation tool.my-first-react-app: your project name (lowercase letters + hyphens recommended).-- --template react: specify the React template.
Interactive process(you will be prompted to choose):
- Project name: press Enter to confirm or modify it.
- Select framework: choose React。
- Select variant:
- 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:
- index.html: The page entry point,
is the React mount point. - src/main.jsx: The React startup file, using React 18's new API to create the root node.
- src/App.jsx: Your main component, with a counter example by default.
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:
import { useState } from 'react': Import the Hook.useState(0): Declare a state variablecountwith an initial value of 0.onClickEvent: Clicking the button updates the state and triggers a re-render.- JSX syntax: Looks like HTML, but is actually JavaScript.
Small Exercises:
- Change the title to your name:
你好,[你的名字]!
- 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:
ReactDOM.createRoot: React 18's new API that enables concurrent features.: Renders your main component.: Strict checks in development mode to help find issues.
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
- CSS: Direct import
./App.css, supports regular CSS. - Picture: Used as a variable after import (Vite automatically optimizes it).
- Advanced: Supports CSS Modules (file name
.module.css), Tailwind CSS (can be configured later).
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.