yggjs_react使用教程 v0.1.1

yggjs_react是一個用于快速創建React項目的工具,它集成了Vite、TypeScript、Zustand和React Router等現代前端技術棧,幫助開發者快速搭建高質量的React應用。

快速入門

快速入門部分將指導您如何安裝yggjs_react工具、創建新項目并啟動開發服務器。

安裝

首先,確保您的系統已安裝Node.js。然后使用npm全局安裝pnpm和yggjs_react:

npm install -g pnpm
npm install -g yggjs_react

查看版本

安裝完成后,可以使用以下命令查看yggjs_react的版本:

ggr --version

創建項目

使用以下命令創建一個新的React項目:

ggr create hello

這將創建一個名為"hello"的新項目目錄。

啟動服務

進入項目目錄并安裝依賴,然后啟動開發服務器:

cd hello
pnpm install --registry=https://registry.npmmirror.com
pnpm dev

這將啟動開發服務器,默認在 http://localhost:5173/ 上運行。

完整代碼

本節詳細展示了項目中各個核心文件的代碼實現。

package.json

項目的配置文件,定義了項目的基本信息、腳本命令和依賴項:

{"name": "hello","private": true,"version": "0.0.0","type": "module","packageManager": "pnpm@8.15.0","scripts": {"dev": "vite","build": "tsc && vite build","lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0","preview": "vite preview"},"dependencies": {"react": "^18.3.1","react-dom": "^18.3.1","react-router-dom": "^6.28.0","zustand": "^5.0.1"},"devDependencies": {"@types/react": "^18.3.23","@types/react-dom": "^18.3.7","@typescript-eslint/eslint-plugin": "^6.21.0","@typescript-eslint/parser": "^6.21.0","@vitejs/plugin-react": "^4.3.4","eslint": "^8.57.1","eslint-plugin-react-hooks": "^4.6.2","eslint-plugin-react-refresh": "^0.4.14","typescript": "^5.9.2","vite": "^4.5.14"},"pnpm": {"registry": "https://registry.npmmirror.com"}
}

tsconfig.json

TypeScript編譯器配置文件,定義了編譯選項和項目包含的文件:

{"compilerOptions": {"target": "ES2020","useDefineForClassFields": true,"lib": ["ES2020","DOM","DOM.Iterable"],"module": "ESNext","skipLibCheck": true,"moduleResolution": "bundler","allowImportingTsExtensions": true,"resolveJsonModule": true,"isolatedModules": true,"noEmit": true,"jsx": "react-jsx","strict": true,"noUnusedLocals": true,"noUnusedParameters": true,"noFallthroughCasesInSwitch": true},"include": ["src"],"references": [{"path": "./tsconfig.node.json"}]
}

tsconfig.node.json

Node.js環境的TypeScript配置文件:

{"compilerOptions": {"composite": true,"skipLibCheck": true,"module": "ESNext","moduleResolution": "bundler","allowSyntheticDefaultImports": true},"include": ["vite.config.ts"]
}

vite.config.ts

Vite構建工具的配置文件:

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'// https://vitejs.dev/config/
export default defineConfig({plugins: [react()],
})

index.html

項目的入口HTML文件:

<!doctype html>
<html lang="en"><head><meta charset="UTF-8" /><link rel="icon" type="image/svg+xml" href="/vite.svg" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>Vite + React + TS</title></head><body><div id="root"></div><script type="module" src="/src/main.tsx"></script></body>
</html>

.npmrc

npm配置文件,用于設置包管理器的行為:

# 使用國內鏡像源加速包下載
registry=https://registry.npmmirror.com# React 相關包的鏡像源
@types:registry=https://registry.npmmirror.com
@typescript-eslint:registry=https://registry.npmmirror.com
@vitejs:registry=https://registry.npmmirror.com# 禁用包鎖定文件的自動更新
package-lock=false# 設置緩存目錄
cache-dir=.pnpm-cache# 啟用嚴格的 peer dependencies 檢查
strict-peer-dependencies=false# 設置網絡超時時間
network-timeout=60000# 啟用進度條
progress=true# 禁用工作區模式,強制在當前目錄安裝依賴
ignore-workspace=true

src/main.tsx

應用的入口文件,負責渲染根組件:

import React from 'react'
import ReactDOM from 'react-dom/client'
import { BrowserRouter } from 'react-router-dom'
import App from './App.tsx'
import './index.css'ReactDOM.createRoot(document.getElementById('root')!).render(<React.StrictMode><BrowserRouter><App /></BrowserRouter></React.StrictMode>,
)

src/index.css

全局樣式文件,定義了應用的基本樣式:

:root {font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;line-height: 1.5;font-weight: 400;color-scheme: light dark;color: rgba(255, 255, 255, 0.87);background-color: #242424;font-synthesis: none;text-rendering: optimizeLegibility;-webkit-font-smoothing: antialiased;-moz-osx-font-smoothing: grayscale;-webkit-text-size-adjust: 100%;
}a {font-weight: 500;color: #646cff;text-decoration: inherit;
}
a:hover {color: #535bf2;
}body {margin: 0;display: flex;place-items: center;min-width: 320px;min-height: 100vh;
}h1 {font-size: 3.2em;line-height: 1.1;
}button {border-radius: 8px;border: 1px solid transparent;padding: 0.6em 1.2em;font-size: 1em;font-weight: 500;font-family: inherit;background-color: #1a1a1a;color: white;cursor: pointer;transition: border-color 0.25s;
}
button:hover {border-color: #646cff;
}
button:focus,
button:focus-visible {outline: 4px auto -webkit-focus-ring-color;
}@media (prefers-color-scheme: light) {:root {color: #213547;background-color: #ffffff;}a:hover {color: #747bff;}button {background-color: #f9f9f9;color: #213547;}
}

src/App.tsx

應用的根組件,組合了布局和路由:

import { Layout } from './components'
import AppRoutes from './routers'
import './App.css'function App() {return (<Layout><AppRoutes /></Layout>)
}export default App

src/App.css

應用根組件的樣式:

#root {max-width: 1280px;margin: 0 auto;padding: 2rem;text-align: center;
}nav {margin-bottom: 2rem;
}nav a {margin: 0 1rem;padding: 0.5rem 1rem;border-radius: 4px;text-decoration: none;
}nav a:hover {background-color: #f0f0f0;
}.card {padding: 2em;
}.card button {margin: 0 0.5rem;
}.card span {margin: 0 1rem;font-size: 1.2em;font-weight: bold;
}

src/routers/index.tsx

應用的路由配置文件:

import { Routes, Route } from 'react-router-dom'
import { Home, About } from '../pages'function AppRoutes() {return (<Routes><Route path="/" element={<Home />} /><Route path="/about" element={<About />} /></Routes>)
}export default AppRoutes

src/store/counter.ts

使用Zustand創建的狀態管理文件:

import { create } from 'zustand'interface CounterState {count: numberincrement: () => voiddecrement: () => void
}export const useCounterStore = create<CounterState>((set) => ({count: 0,increment: () => set((state) => ({ count: state.count + 1 })),decrement: () => set((state) => ({ count: state.count - 1 })),
}))

src/components/Layout.tsx

應用的布局組件:

import { Link } from 'react-router-dom'
import { ReactNode } from 'react'interface LayoutProps {children: ReactNode
}function Layout({ children }: LayoutProps) {return (<div className="App"><nav><Link to="/">Home</Link><Link to="/about">About</Link></nav><main>{children}</main></div>)
}export default Layout

src/components/index.ts

組件導出文件:

// 組件導出
export { default as Layout } from './Layout'

src/pages/index.ts

頁面組件導出文件:

// 頁面組件導出
export { default as Home } from './Home'
export { default as About } from './About'

src/pages/Home.tsx

首頁組件,展示了計數器功能:

import { useCounterStore } from '../store/counter'function Home() {const { count, increment, decrement } = useCounterStore()return (<div><h1>Home Page</h1><div className="card"><button onClick={decrement}>-</button><span>count is {count}</span><button onClick={increment}>+</button></div></div>)
}export default Home

src/pages/About.tsx

關于頁面組件:

function About() {return (<div><h1>About Page</h1><p>This is a basic React template with Vite, TypeScript, Zustand, and React Router.</p></div>)
}export default About

啟動服務

在項目根目錄下執行以下命令安裝依賴并啟動開發服務器:

pnpm i
pnpm dev

瀏覽器訪問

服務啟動后,可以在瀏覽器中訪問以下地址:

  • 主頁: http://localhost:5173/
  • 關于頁面: http://localhost:5173/about

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/919661.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/919661.shtml
英文地址,請注明出處:http://en.pswp.cn/news/919661.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

vulhub可用的docker源

這一塊不太容易找&#xff0c;我試了好幾個源&#xff0c;下面是20250820測試可用源 編輯方法sudo mkdir -p /etc/docker sudo vim /etc/docker/daemon.json 配置內容 [1] {"registry-mirrors" : ["https://docker.registry.cyou", "https://docker-…

基于YOLOv8-SEAttention與LLMs融合的農作物害蟲智能診斷與防控決策系統

1. 引言 1.1 研究背景與意義 農作物蟲害是制約農業產量與質量的重要因素。據FAO報告&#xff0c;全球每年因病蟲害造成的糧食損失高達 20%–40%。傳統人工巡查與經驗診斷具有時效性差、成本高與專業人才不足等缺陷。近年來&#xff0c;計算機視覺特別是目標檢測技術在農業檢測…

從零開始構建GraphRAG紅樓夢知識圖譜問答項目(三)

文章結尾有CSDN官方提供的學長的聯系方式&#xff01;&#xff01; 歡迎關注B站從零開始構建一個基于GraphRAG的紅樓夢項目 第三集01 搭建后端服務 創建一個python文件server.py 完整源碼放到文章最后了。 1.1 graphrag 相關導入 # GraphRAG 相關導入 from graphrag.query.cont…

S32K328(Arm Cortex-M7)適配CmBacktrace錯誤追蹤

CmBacktrace 相當于重寫了hard_fault函數&#xff0c;在hard_fault函數里面去分析SCB寄存器的信息和堆棧信息&#xff0c;然后把這些信息打印出來(或者寫到flash)&#xff1b;通過使用串口輸出產生hard_fault的堆棧信息&#xff0c;然后利用addr2line工具反推出具體的代碼執行函…

AI研究引擎的簡單技術實現步驟

產品愿景與核心功能 1.1 產品使命 “洞見 Weaver”是一個全棧AI Web應用,旨在將用戶的復雜研究問題,通過AI驅動的動態思維導圖和結構化報告,轉化為一次沉浸式的、可追溯的視覺探索之旅。我們的使命是,將AI復雜的推理過程透明化,將人類的探索直覺與AI的分析能力無縫結合,…

open webui源碼分析5-Tools

本文從最簡單的時間工具入手&#xff0c;分析Tools相關的代碼。一、安裝工具git clone https://github.com/open-webui/openapi-servers cd openapi-servers# 進入時間工具目錄 cd servers/timepip install -r requirements.txt# 啟動服務 uvicorn main:app --host 0.0.0.0 --r…

windows下通過vscode遠程調試linux c/cpp程序配置

windows下通過vscode遠程調試linux c/cpp程序配置vscode插件配置linux依賴工具安裝launch.json配置vscode插件配置 CodeLLDB插件需要提前下載&#xff1a; linux依賴工具安裝 sudo apt update sudo apt install cmake clangdlaunch.json配置 {"version": "0…

IDEA報JDK版本問題

解決思路&#xff1a;1.找到配置jdk的IDEA配置位置settings和project structure2.先配置setting3.再修改項目結構

VirtualBox 安裝 Ubuntu Server 系統及 Ubuntu 初始配置

文章目錄簡介VirtualBoxUbuntu Server 簡介Ubuntu Server 下載安裝 Ubuntu Server首選項配置導入系統鏡像配置系統用戶配置內存 CPU 虛擬硬盤開始安裝 Ubuntu安裝完成登錄系統配置網絡Ubuntu 系統配置安裝常用工具安裝 SSH設置 root 密碼配置 IP 地址&#xff08;推薦自動分配I…

Milvus 可觀測性最佳實踐

Milvus 介紹 Milvus 是一個開源的向量數據庫&#xff0c;專為處理大規模、高維度向量數據而設計&#xff0c;廣泛應用于人工智能、推薦系統、圖像檢索、自然語言處理等場景。它支持億級向量的高效存儲與快速檢索&#xff0c;內置多種相似度搜索算法&#xff08;如 HNSW、IVF、…

arcgis-空間矯正工具(將下發數據A的信息放置原始數據B的原始信息并放置到成果數據C中,主要按下發數據A的范圍)

正常來說&#xff0c;可以直接相交獲取&#xff0c;但是會存在原始數據B將下發數據A進行分割&#xff0c;所以相交功能會導致最終成果會產生稀碎圖斑及圖斑切割&#xff0c;因此&#xff0c;經學習了解&#xff0c;學會此方法進行既保留原始數據B的信息&#xff0c;又按下發數據…

MySQL深分頁慢問題及性能優化

在數據驅動的應用中&#xff0c;分頁是不可或缺的功能。然而&#xff0c;當數據量達到百萬甚至千萬級別時&#xff0c;傳統基于 LIMIT OFFSET 的分頁方式會遭遇嚴重的性能瓶頸&#xff0c;即“深分頁”問題。本文將剖析其根源并提供主流的優化策略。問題根源&#xff1a;LIMIT …

漫談《數字圖像處理》之平滑

在數字圖像處理中&#xff0c;平滑&#xff08;Smoothing&#xff09; 的核心目標是降低圖像噪聲、模糊細節或簡化紋理&#xff0c;本質是通過 “局部鄰域運算” 對像素值進行 “平均化” 或 “規整化”&#xff0c;讓圖像整體更 “平緩”。形態學平滑與高斯平滑、均值平滑等其…

機器學習之數據預處理學習總結

在機器學習中&#xff0c;數據預處理是模型訓練前至關重要的環節&#xff0c;直接影響模型的性能和準確性。通過本次學習&#xff0c;我系統掌握了數據預處理的核心方法與工具&#xff0c;現將主要內容總結如下&#xff1a;一、缺失值處理缺失值是實際數據中常見的問題&#xf…

在完全沒有無線網絡(Wi-Fi)和移動網絡(蜂窩數據)的環境下,使用安卓平板,通過USB數據線(而不是Wi-Fi)來控制電腦(版本2)

在完全沒有無線網絡&#xff08;Wi-Fi&#xff09;和移動網絡&#xff08;蜂窩數據&#xff09;的環境下&#xff0c;要實現用安卓手機通過USB數據線控制電腦&#xff0c;核心思路是&#xff1a;利用USB數據線創建一個純粹的、本地的有線網絡連接。 這不僅是可行的&#xff0c;…

Ubuntu22.04配置網絡上網

前言 安裝Ubuntu系統后&#xff0c;有時會遇到無法聯網、無法使用瀏覽器的問題。然而當宿主機已連接網絡時&#xff0c;虛擬機通常也能聯網&#xff0c;需要進行一些配置&#xff0c;現在就以Ubuntu22.04為例。 VMware配置打開虛擬網絡編輯器 啟動VMWare點擊編輯&#xff0c;并…

網絡協議之TCP和UDP

寫在前面 本文來看下TCP和UDP協議。 我們接觸這兩個協議最多的應該就是在面試中了&#xff0c;經典題目就是“TCP和UDP有什么區別&#xff1f;”&#xff0c;而最常得到的答案就是TCP是面向連接的&#xff0c;而UDP是面向無連接的。 那么這里的連接到底是什么呢&#xff1f;難…

Qt音樂播放器項目實踐:本地持久化與邊角問題處理

本音樂播放器完整項目源碼(包含各個按鈕的圖片文件): ly/Project-Code - Gitee.com 一.本地持久化 請注意&#xff0c;學習此部分之前需要讀者具有一定的Mysql基礎。如果讀者能夠接受無法本地持久化&#xff0c;那么可以跳過這部分內容&#xff0c;直接去看邊角問題處理。我…

基于NB-IoT技術的寵物定位跟蹤系統設計#基于STM32\物聯網\單片機技術的寵物定位跟蹤系統

基于NB-IoT技術的寵物定位跟蹤系統設計#基于STM32\物聯網\單片機技術的寵物定位跟蹤系統在設計基于NB-IoT技術的寵物定位跟蹤系統時&#xff0c;首先明確了系統分為感知層、網絡層和應用層三個部分。在感知層&#xff0c;考慮到需要獲取寵物位置和運動狀態&#xff0c;選用GPS定…

【入門級-算法-3、基礎算法:遞歸法】

遞歸是一種非常重要的算法思想&#xff0c;它指的是函數調用自身的過程。遞歸通常包含兩個主要部分&#xff1a;基線條件&#xff08;終止條件&#xff09;和遞歸條件&#xff08;調用自身的條件&#xff09;。 下面通過例子來理解遞歸算法&#xff1a; 計算階乘 階乘的遞歸定義…