React 腳手架使用指南(最新推薦)
React 腳手架使用指南
React 腳手架(Create React App)是 Facebook 官方提供的創(chuàng)建 React 單頁應(yīng)用的工具。它提供了一個零配置的現(xiàn)代構(gòu)建設(shè)置。
為什么使用腳手架?
- 無需配置 Webpack 和 Babel
- 內(nèi)置熱重載
- 自動化構(gòu)建過程
- 優(yōu)化的生產(chǎn)構(gòu)建
- 良好的開發(fā)體驗
創(chuàng)建項目
1. 使用 npx 創(chuàng)建(推薦)
# 創(chuàng)建基礎(chǔ) React 項目 npx create-react-app my-app # 創(chuàng)建 TypeScript 項目 npx create-react-app my-app --template typescript # 進(jìn)入項目目錄 cd my-app # 啟動開發(fā)服務(wù)器 npm start
2. 使用 npm 創(chuàng)建
# 全局安裝 create-react-app npm install -g create-react-app # 創(chuàng)建項目 create-react-app my-app
項目結(jié)構(gòu)
my-app/ ├── node_modules/ ├── public/ │ ├── favicon.ico │ ├── index.html │ ├── manifest.json │ └── robots.txt ├── src/ │ ├── App.css │ ├── App.tsx │ ├── App.test.tsx │ ├── index.css │ ├── index.tsx │ ├── logo.svg │ ├── react-app-env.d.ts │ ├── reportWebVitals.ts │ └── setupTests.ts ├── .gitignore ├── package.json ├── README.md ├── tsconfig.json └── yarn.lock
目錄說明
1. public 目錄
<!-- public/index.html - 應(yīng)用的 HTML 模板 -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" rel="external nofollow" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>React App</title>
</head>
<body>
<div id="root"></div>
</body>
</html>2. src 目錄
// src/index.tsx - 應(yīng)用的入口文件
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.tsx - 主應(yīng)用組件
import React from 'react';
import './App.css';
function App() {
return (
<div className="App">
<header className="App-header">
<h1>Welcome to React</h1>
</header>
</div>
);
}
export default App;常用命令
開發(fā)命令
# 啟動開發(fā)服務(wù)器 npm start # 構(gòu)建生產(chǎn)版本 npm run build # 運行測試 npm test # 彈出配置文件 npm run eject
配置說明
1. package.json
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"@types/node": "^16.18.0",
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"typescript": "^4.8.4",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
}2. tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": ["src"]
}最佳實踐
1. 目錄結(jié)構(gòu)建議
src/ ├── components/ # 共享組件 ├── pages/ # 頁面組件 ├── hooks/ # 自定義 hooks ├── services/ # API 服務(wù) ├── utils/ # 工具函數(shù) ├── types/ # TypeScript 類型定義 ├── assets/ # 靜態(tài)資源 └── styles/ # 樣式文件
2. 環(huán)境變量配置
# .env.development - 開發(fā)環(huán)境配置 REACT_APP_API_URL=http://dev-api.example.com # .env.production - 生產(chǎn)環(huán)境配置 REACT_APP_API_URL=http://api.example.com
3. 代碼規(guī)范配置
// .eslintrc
{
"extends": [
"react-app",
"react-app/jest"
],
"rules": {
"no-console": "warn",
"prefer-const": "error"
}
}常見問題解決
1. 路徑別名配置
// tsconfig.json
{
"compilerOptions": {
"baseUrl": "src",
"paths": {
"@/*": ["*"],
"@components/*": ["components/*"],
"@pages/*": ["pages/*"]
}
}
}2. 代理配置
// package.json
{
"proxy": "http://localhost:3001"
}總結(jié)
腳手架優(yōu)勢:
- 快速創(chuàng)建項目
- 零配置開始
- 完整的開發(fā)環(huán)境
- 優(yōu)化的構(gòu)建過程
使用建議:
- 遵循目錄結(jié)構(gòu)規(guī)范
- 合理使用環(huán)境變量
- 保持代碼規(guī)范統(tǒng)一
- 適時添加必要的配置
注意事項:
- 慎用 eject
- 及時更新依賴
- 保持項目結(jié)構(gòu)清晰
- 做好文檔維護
到此這篇關(guān)于React 腳手架使用指南的文章就介紹到這了,更多相關(guān)React 腳手架使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一文搞懂?React?18?中的?useTransition()?與?useDeferredValue()
這篇文章主要介紹了一文搞懂?React?18?中的?useTransition()與useDeferredValue(),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09
在React中用canvas對圖片標(biāo)注的實現(xiàn)
本文主要介紹了在React中用canvas對圖片標(biāo)注的實現(xiàn) ,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
Create?react?app修改webapck配置導(dǎo)入文件alias
這篇文章主要為大家介紹了Create?react?app修改webapck配置導(dǎo)入文件alias,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12

