React 腳手架使用指南(最新推薦)
React 腳手架使用指南
React 腳手架(Create React App)是 Facebook 官方提供的創(chuàng)建 React 單頁(yè)應(yīng)用的工具。它提供了一個(gè)零配置的現(xiàn)代構(gòu)建設(shè)置。
為什么使用腳手架?
- 無(wú)需配置 Webpack 和 Babel
- 內(nèi)置熱重載
- 自動(dòng)化構(gòu)建過(guò)程
- 優(yōu)化的生產(chǎn)構(gòu)建
- 良好的開(kāi)發(fā)體驗(yàn)
創(chuàng)建項(xiàng)目
1. 使用 npx 創(chuàng)建(推薦)
# 創(chuàng)建基礎(chǔ) React 項(xiàng)目 npx create-react-app my-app # 創(chuàng)建 TypeScript 項(xiàng)目 npx create-react-app my-app --template typescript # 進(jìn)入項(xiàng)目目錄 cd my-app # 啟動(dòng)開(kāi)發(fā)服務(wù)器 npm start
2. 使用 npm 創(chuàng)建
# 全局安裝 create-react-app npm install -g create-react-app # 創(chuàng)建項(xiàng)目 create-react-app my-app
項(xiàng)目結(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
目錄說(shuō)明
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;
常用命令
開(kāi)發(fā)命令
# 啟動(dòng)開(kāi)發(fā)服務(wù)器 npm start # 構(gòu)建生產(chǎn)版本 npm run build # 運(yùn)行測(cè)試 npm test # 彈出配置文件 npm run eject
配置說(shuō)明
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"] }
最佳實(shí)踐
1. 目錄結(jié)構(gòu)建議
src/ ├── components/ # 共享組件 ├── pages/ # 頁(yè)面組件 ├── hooks/ # 自定義 hooks ├── services/ # API 服務(wù) ├── utils/ # 工具函數(shù) ├── types/ # TypeScript 類型定義 ├── assets/ # 靜態(tài)資源 └── styles/ # 樣式文件
2. 環(huán)境變量配置
# .env.development - 開(kāi)發(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" } }
常見(jiàn)問(wèn)題解決
1. 路徑別名配置
// tsconfig.json { "compilerOptions": { "baseUrl": "src", "paths": { "@/*": ["*"], "@components/*": ["components/*"], "@pages/*": ["pages/*"] } } }
2. 代理配置
// package.json { "proxy": "http://localhost:3001" }
總結(jié)
腳手架優(yōu)勢(shì):
- 快速創(chuàng)建項(xiàng)目
- 零配置開(kāi)始
- 完整的開(kāi)發(fā)環(huán)境
- 優(yōu)化的構(gòu)建過(guò)程
使用建議:
- 遵循目錄結(jié)構(gòu)規(guī)范
- 合理使用環(huán)境變量
- 保持代碼規(guī)范統(tǒng)一
- 適時(shí)添加必要的配置
注意事項(xiàng):
- 慎用 eject
- 及時(shí)更新依賴
- 保持項(xiàng)目結(jié)構(gòu)清晰
- 做好文檔維護(hù)
到此這篇關(guān)于React 腳手架使用指南的文章就介紹到這了,更多相關(guān)React 腳手架使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一文搞懂?React?18?中的?useTransition()?與?useDeferredValue()
這篇文章主要介紹了一文搞懂?React?18?中的?useTransition()與useDeferredValue(),文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09在React中用canvas對(duì)圖片標(biāo)注的實(shí)現(xiàn)
本文主要介紹了在React中用canvas對(duì)圖片標(biāo)注的實(shí)現(xiàn) ,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05react組件實(shí)例屬性props實(shí)例詳解
這篇文章主要介紹了react組件實(shí)例屬性props,本文結(jié)合實(shí)例代碼給大家簡(jiǎn)單介紹了props使用方法,代碼簡(jiǎn)單易懂,需要的朋友可以參考下2023-01-01react項(xiàng)目如何運(yùn)行在微信公眾號(hào)
這篇文章主要介紹了react項(xiàng)目如何運(yùn)行在微信公眾號(hào),幫助大家更好的理解和學(xué)習(xí)使用react,感興趣的朋友可以了解下2021-04-04使用react-color實(shí)現(xiàn)前端取色器的方法
本文通過(guò)代碼給大家介紹了使用react-color實(shí)現(xiàn)前端取色器的方法,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-11-11Create?react?app修改webapck配置導(dǎo)入文件alias
這篇文章主要為大家介紹了Create?react?app修改webapck配置導(dǎo)入文件alias,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12