ããã¯ããªã«ãããããŠæžãããã®ïŒ
å°ããReactãæ±ã£ãŠã¿ãããšæããŸããŠã
ãšã¯ãããã©ãããå ¥ã£ããããã®ããªïŒãšããããšã§ãä»åã¯Viteãè©ŠããŠã¿ãããšã«ããŸããã
ããŸãReactèªäœã¯åºãŠããªããã§ããã©ã
ä»ã®Reactã®å§ãæ¹
ããã§Reactã®æ°ãããããžã§ã¯ããå§ãæ¹ãèŠãŠã¿ãŸãã
If you want to build a new app or a new website fully with React, we recommend picking one of the React-powered frameworks popular in the community. Frameworks provide features that most apps and sites eventually need, including routing, data fetching, and generating HTML.
Start a New React Project – React
ä»ã¯ReactãããŒã¹ã«ãããã¬ãŒã ã¯ãŒã¯ãå©çšããããšãå§ããããŠããããã§ãã
ãã®ããŒãžã§ã¯ãNext.jsãRemixãGatsbyãExpoïŒãã€ãã£ãã¢ããªåãïŒãæããããŠããŸãã
å人çã«ã¯ãŸãã¯ãã¬ãŒã ã¯ãŒã¯ãªãã®äœ¿ãæ¹ãç¥ãããã®ã§ãããããã«ã€ããŠã¯ãã¬ãŒã ã¯ãŒã¯ã®çŽ¹ä»ã®åŸã«é ããŠããŸãã
ãããéããŠã¿ããšããã¬ãŒã ã¯ãŒã¯ã䜿ããªããŠãã§ããããã©ãã¬ãŒã ã¯ãŒã¯ã®å©çšãå§ãããšæžãããŠããŸãã
å¿
èŠã«ãªããã®ããããã足ããŠãªãããã»ããã¢ãããå¿
èŠã«ãªãããã©ããã§ãé 匵ãã®ãªãã©ããïŒãšããæãã§ãã
You can definitely use React without a frameworkâthatâs how youâd use React for a part of your page. However, if youâre building a new app or a site fully with React, we recommend using a framework.
Hereâs why.
ãçç¥ã
If youâre still not convinced, or your app has unusual constraints not served well by these frameworks and youâd like to roll your own custom setup, we canât stop youâgo for it! Grab react and react-dom from npm, set up your custom build process with a bundler like Vite or Parcel, and add other tools as you need them for routing, static generation or server-side rendering, and more.
Start a New React Project / Can I use React without a framework?
ããã§ãViteãšParcelãåºãŠããŸãã
Vite | Next Generation Frontend Tooling
ãå§ãã¯ããªããããšã¯èšãããŠãããã®ã®ããŸãã¯Reactã«ãã©ãŒã«ã¹ãããã®ã§Viteãéžãã§ã¿ãŸãã
Vite
Viteã®ãªãã£ã·ã£ã«ãµã€ãã¯ãã¡ãã§ã次äžä»£ã®ããã³ããšã³ãããŒã«ã§ãããšæžãããŠããŸãã
Vite | Next Generation Frontend Tooling
The State of JS 2022ã®ãã«ãããŒã«ã®ã«ããŽãªãŒã§ã人æ°ã®ããã§ãã
State of JavaScript 2022: Build Tools
ãªãViteãè¯ãã®ãïŒãšããããŒãžã¯ããã¡ãã
ãããèŠããšã以äžã®ç¹åŸŽã謳ãããŠããŸãã
- éçºãµãŒããŒã®èµ·åæéã®ççž®
- æŽæ°ïŒHot Module ReplacementïŒHMRïŒã®é«éå
- æ¬çªç°å¢åãã®ãã³ãã«
æ©èœã«ã€ããŠã¯ãã¡ãã
- ã¢ãžã¥ãŒã«ã€ã³ããŒã
- Hot Module ReplacementïŒHMRïŒ
- TypeScriptã®ãµããŒã
- VueãJSXãCSS
- Static AssetsãJSONã®ã€ã³ããŒã
- Globã€ã³ããŒããåçã€ã³ããŒããWebAssemblyã®å©çš
- Web Workerã®ãµããŒã
- ãã«ãã®æé©å
TypeScriptã®ãµããŒãã«ã€ããŠ
TypeScriptã®ãµããŒãã«ã€ããŠã¯ã¡ãã£ãšæ°ã«ãªãã®ã§ãããå°ãèŠãŠãããŸãã
Viteã¯.ts
ãã¡ã€ã«ã®ãã©ã³ã¹ãã€ã«ã®ã¿ãå®è¡ããåãã§ãã¯ã¯è¡ããŸãããåãã§ãã¯ã¯IDEããã«ãããã»ã¹ã§ç¢ºèªããããšã
æ³å®ããŠããããã§ãã
Note that Vite only performs transpilation on .ts files and does NOT perform type checking. It assumes type checking is taken care of by your IDE and build process.
Features / TypeScript / Transpile Only
isolatedModules
ãuseDefineForClassFields
ã«ã¯æ³šæããå¿
èŠãããããã§ãã
Features / TypeScript / TypeScript Compiler Options
ãã®ä»ã圱é¿ãäžãããã®ãšããŠextends
ãimportsNotUsedAsValues
ãpreserveValueImports
ãjsxFactory
ãjsxFragmentFactory
ã
æããããŠããŸãã
Viteã¯ããã©ã«ãã§Node.js APIã䜿çšãããããã¯ã©ã€ã¢ã³ãåŽã®ã³ãŒãã§ããããšã瀺ãããã«ã以äžã®å®£èšãè¿œå ããå¿
èŠã
ããããã§ãã
/// <reference types="vite/client" />
ããã¯tsconfig.json
ã§ãèšå®ã§ããããã§ãã
{ "compilerOptions": { "types": ["vite/client"] } }
Features / TypeScript / Client Types
Create React Appã¯ïŒ
ãšããã§ãReactãå§ããã®ã«äœ¿ãã®ã¯Create React Appã ãšæãã®ã§ããããã¡ãã¯ã©ããªã£ãã®ã§ãããïŒ
Reactã®ãã¡ãã®ããŒãžã«ã¯ååããããŸããã§ããã
Start a New React Project – React
çŸæç¹ã§ã®æçµãªãªãŒã¹ã¯ã2022幎4æã®5.0.1ã§ãã
Release v5.0.1 · facebook/create-react-app · GitHub
以äžããããåèã«ã
We need regualr CRA maintainer · Issue #11180 · facebook/create-react-app · GitHub
ä»åŸã¯èŠãªããªã£ãŠããããŒã«ã«ãªã£ãŠããããã§ããã
話ãæ»ããŠãä»åã¯Viteã䜿ã£ãŠã¿ãŸãã
ç°å¢
ä»åã®ç°å¢ã¯ããã¡ãã
$ node --version v18.17.1 $ npm --version 9.6.7
Viteãå§ããŠã¿ã
ãã¡ããèŠãªãããViteãå§ããŠã¿ãŸãã
ãŸãã¯Viteãããžã§ã¯ããäœæããããã§ãã
ãã³ãã¬ãŒãããµããŒãããŠããããã§ãJavaScriptãTypeScriptã§ä»¥äžãããããã§ãã
JavaScript | TypeScript |
---|---|
vanilla | vanilla-ts |
vue | vue-ts |
react | react-ts |
preact | preact-ts |
lit | lit-ts |
svelte | velte-ts |
solid | solid-ts |
qwik | qwik-ts |
ã³ãã¥ããã£ãã¡ã³ããã³ã¹ããŠãããã³ãã¬ãŒããããããã§ãã
GitHub - vitejs/awesome-vite: ⚡️ A curated list of awesome things related to Vite.js
ä»åã¯ãReactïŒTypeScriptïŒreact-ts
ïŒãéžãã§ã¿ãŸãã
$ npm create vite vite-getting-started -- --template react-ts
åºåçµæã
Scaffolding project in /path/to/vite-getting-started... Done. Now run: cd vite-getting-started npm install npm run dev
ãããžã§ã¯ããã§ãããããªã®ã§ããã£ã¬ã¯ããªå ã«ç§»åããŠã¿ãŸãã
$ cd vite-getting-started
ãã£ã¬ã¯ããªå ã®æ§æã
$ tree . âââ README.md âââ index.html âââ package.json âââ public â  âââ vite.svg âââ src â  âââ App.css â  âââ App.tsx â  âââ assets â  â  âââ react.svg â  âââ index.css â  âââ main.tsx â  âââ vite-env.d.ts âââ tsconfig.json âââ tsconfig.node.json âââ vite.config.ts 3 directories, 13 files
åçš®èšå®ãã¡ã€ã«ãèŠãŠã¿ãŸãããã
package.json
{ "name": "vite-getting-started", "private": true, "version": "0.0.0", "type": "module", "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.2.0", "react-dom": "^18.2.0" }, "devDependencies": { "@types/react": "^18.2.15", "@types/react-dom": "^18.2.7", "@typescript-eslint/eslint-plugin": "^6.0.0", "@typescript-eslint/parser": "^6.0.0", "@vitejs/plugin-react": "^4.0.3", "eslint": "^8.45.0", "eslint-plugin-react-hooks": "^4.6.0", "eslint-plugin-react-refresh": "^0.4.3", "typescript": "^5.0.2", "vite": "^4.4.5" } }
tsconfig.json
{ "compilerOptions": { "target": "ES2020", "useDefineForClassFields": true, "lib": ["ES2020", "DOM", "DOM.Iterable"], "module": "ESNext", "skipLibCheck": true, /* Bundler mode */ "moduleResolution": "bundler", "allowImportingTsExtensions": true, "resolveJsonModule": true, "isolatedModules": true, "noEmit": true, "jsx": "react-jsx", /* Linting */ "strict": true, "noUnusedLocals": true, "noUnusedParameters": true, "noFallthroughCasesInSwitch": true }, "include": ["src"], "references": [{ "path": "./tsconfig.node.json" }] }
tsconfig.node.json
{ "compilerOptions": { "composite": true, "skipLibCheck": true, "module": "ESNext", "moduleResolution": "bundler", "allowSyntheticDefaultImports": true }, "include": ["vite.config.ts"] }
vite.config.ts
import { defineConfig } from 'vite' import react from '@vitejs/plugin-react' // https://vitejs.dev/config/ export default defineConfig({ plugins: [react()], })
ãœãŒã¹ã³ãŒãã®äžéšãReactã䜿ã£ããã®ã§ããã
src/main.tsx
import React from 'react' import ReactDOM from 'react-dom/client' import App from './App.tsx' import './index.css' ReactDOM.createRoot(document.getElementById('root')!).render( <React.StrictMode> <App /> </React.StrictMode>, )
src/App.tsx
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> <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>Vite + React</h1> <div className="card"> <button onClick={() => setCount((count) => count + 1)}> count is {count} </button> <p> Edit <code>src/App.tsx</code> and save to test HMR </p> </div> <p className="read-the-docs"> Click on the Vite and React logos to learn more </p> </> ) } export default App
ãã¡ãã¯ãã¯ã©ã€ã¢ã³ãåŽã§ããããšã宣èšãããã¡ã€ã«ã§ããã
src/vite-env.d.ts
/// <reference types="vite/client" />
Features / TypeScript / Client Types
äŸåé¢ä¿ãã€ã³ã¹ããŒã«ã
$ npm i
éçºãµãŒããŒãèµ·åã
$ npm run dev
å®äœã¯vite
ã³ãã³ãã§ããã
"scripts": { "dev": "vite", "build": "tsc && vite build", "lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0", "preview": "vite preview" },
éçºãµãŒããŒãèµ·åããããã§ãã
VITE v4.4.9 ready in 970 ms â Local: http://localhost:5173/ â Network: use --host to expose â press h to show help
確èªããŠã¿ãŸããhttp://localhost:5173
ãžã¢ã¯ã»ã¹ããŠã¿ãŸãã
åäœããŠããã®ã確èªã§ããŸããã
ã¯ãªãã¯ã§ã«ãŠã³ãã¢ãããããµã³ãã«ãªã®ã§ãå°ãéãã§ã¿ãŸãã
src/vite-env.d.ts
ããã§ãéçºãµãŒããŒã1床æ¢ããŸãã
ä»åºŠã¯ãããã¯ã·ã§ã³çšã«ãã«ãããŠã¿ãŸãããã
$ npm run build
åºåçµæã
> vite-getting-started@0.0.0 build > tsc && vite build vite v4.4.9 building for production... â 34 modules transformed. dist/index.html 0.46 kB â gzip: 0.30 kB dist/assets/react-35ef61ed.svg 4.13 kB â gzip: 2.14 kB dist/assets/index-d526a0c5.css 1.42 kB â gzip: 0.74 kB dist/assets/index-c7e05d32.js 143.41 kB â gzip: 46.10 kB â built in 1.37s
dist
ãã£ã¬ã¯ããªå
ã«ãã¡ã€ã«ãçæãããããã§ãã
$ tree dist dist âââ assets â  âââ index-d526a0c5.css â  âââ index-c7e05d32.js â  âââ react-35ef61ed.svg âââ index.html âââ vite.svg 1 directory, 5 files
ãã¡ãã確èªããŠã¿ãŸããããPythonã®HTTPãµãŒããŒãèµ·åã
$ python3 -m http.server --directory dist
http://localhost:8000/
ãžã¢ã¯ã»ã¹ããŠç¢ºèªã
OKã§ããã
HMRã確èªããŠã¿ãŸããéçºãµãŒããŒãèµ·åã
$ npm run dev
App.tsx
ã以äžã®ããã«ä¿®æ£ã
<h1>Vite + React</h1>
ãããšãã³ã³ãœãŒã«ã«ä»¥äžã®ããã«è¡šç€ºãã
22:14:16 [vite] hmr update /src/App.tsx
å€æŽãåæ ãããããšã確èªã§ããŸããã
ããšã¯åãã§ãã¯ã§ãããnpm run build
ã®æã¯tsc
ã§ç¢ºèªããŠãããŸãããéçºãµãŒããŒã䜿ã£ãŠããæã¯ããã¯ãããŸããã
tsc --watch
ãè¿œå ããŠããã®ãããã§ããããã
"scripts": { "dev": "vite", "build": "tsc && vite build", "lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0", "preview": "vite preview", "typecheck:watch": "tsc --watch" },
ãããªæãã§ã
$ npm run typecheck:watch
ãŸãšã
Viteã§Reactã䜿ã£ããããžã§ã¯ããå§ããŠã¿ãŸããããã®ããããå
šç¶è¿œã£ãŠããªãã®ã§ãã¬ã³ããšãããæ¹ãããŸã²ãšã€ããããŸããã
ãŸããã¡ãã£ãšãã€è¿œã£ãŠãããã°ããããªãšæããŸãã
æ°é·ã«ãã£ãŠãããããªãšã