npm create vite@latest my-"> npm create vite@latest my-"> npm create vite@latest my-">
React 词条目录
前端Wiki/React/实战/工程化起步

脚手架与项目结构

创建 React 项目的事实标准是 Vite:秒级启动、按需编译。命令为 npm create vite@latest my-app -- --template react,然后 npm install、npm run dev。CRA(create-react-app)官方已停止维护,新项目不要再选它。

典型目录:src/main.jsx 是入口(createRoot 在此挂载),src/App.jsx 是根组件,src/components/ 放组件,src/hooks/ 放自定义 Hook,src/pages/ 放路由页面。模板加 --template react-ts 可获得 TypeScript 版本。

  • 新项目首选 Vite:npm create vite@latest -- --template react
  • 入口 main.jsx → createRoot(document.getElementById('root')).render()
  • CRA 已停维,仅维护中的老项目会见到
BASH
# 1. 创建项目(选 react 模板;要 TS 选 react-ts)
npm create vite@latest my-app -- --template react

# 2. 安装依赖并启动
cd my-app
npm install
npm run dev          # 开发:http://localhost:5173

# 3. 常用依赖按需加
npm install react-router-dom     # 路由
npm install axios                # 请求
npm install zustand              # 状态管理
npm install -D tailwindcss       # 原子 CSS

# 4. 打包上线
npm run build        # 产物在 dist/
npm run preview      # 本地预览打包结果
本节要点脚手架认准 Vite:create vite、install、dev 三步起项目;目录按 components/pages/hooks 分。