コンテンツにスキップ

Reactのプロジェクトの作成

Reactのプロジェクトの作成

この節では、React のプロジェクトの作り方について説明します。

Viteを使ったプロジェクトの作成

Section titled “Viteを使ったプロジェクトの作成”

Vite というツールを使って React のプロジェクトを作成します。

コマンドを実行した階層にプロジェクトができます。

ターミナルウィンドウ
> npm create vite@latest my-react-app -- --template react
Scaffolding project in /path/to/my-react-app...
Done. Now run:
cd my-react-app
npm install
npm run dev

次のようなプロジェクトが作成されます。

./
├── .gitignore
├── .oxlintrc.json
├── README.md
├── index.html
├── package.json
├── public
│ ├── favicon.svg
│ └── icons.svg
├── src
│ ├── App.css
│ ├── App.jsx
│ ├── assets
│ │ ├── hero.png
│ │ ├── react.svg
│ │ └── vite.svg
│ ├── index.css
│ └── main.jsx
└── vite.config.js

まずは作成されたプロジェクトを起動してみましょう。

依存パッケージのインストール

Section titled “依存パッケージのインストール”

React を使ったプロジェクトでは自分が書いたコード以外にいくつかのパッケージを使います。

まずは利用するパッケージをインストールします。

作成したプロジェクトに移動して npm install を実行します。

ターミナルウィンドウ
> cd my-react-app
> npm install

利用しているパッケージは言い換えればないと動かない必要なパッケージです。これらのパッケージのことを依存パッケージといいます。 場合によってはこれらのことをライブラリと呼ぶこともあります。様々な呼び方がありますが、混乱しないように気をつけてください。

次のコマンドで開発用のサーバーが起動します。

ターミナルウィンドウ
> npm run dev
VITE v6.1.0 ready in 209 ms
➜ Local: http://localhost:5173/
➜ Network: use --host to expose
➜ press h + enter to show help

起動しない場合は依存パッケージがインストールされているか確認してください。

ブラウザで http://localhost:5173/ にアクセスしてみましょう。

ページが表示されれば成功です。

最低元の構成にしていきます。

次のファイルを削除します。

  • public/favicon.svg
  • public/icons.svg
  • src/assets/hero.png
  • src/assets/react.svg
  • src/assets/vite.svg
  • src/App.css
  • src/index.css

中身が空になった public と src/assets のフォルダも削除します。

削除すると次のような構造になります。

./
├── .gitignore
├── .oxlintrc.json
├── README.md
├── index.html
├── package-lock.json
├── package.json
├── src
│ ├── App.jsx
│ └── main.jsx
└── vite.config.js

削除したファイルを参照している箇所を修正します。

各ファイルを次のように修正します。

src/App.jsx
1
import { useState } from 'react'
2
import heroImg from './assets/hero.png'
3
import reactLogo from './assets/react.svg'
4
import viteLogo from './assets/vite.svg'
5
import './App.css'
6
7
function App() {
8
const [count, setCount] = useState(0)
9
10
return (
11
<>
12
<section id="center">
13
<div className="hero">
14
<img src={heroImg} className="base" width="170" height="179" alt="" />
15
<img src={reactLogo} className="framework" alt="React logo" />
16
<img src={viteLogo} className="vite" alt="Vite logo" />
17
</div>
18
<div>
19
<h1>Get started</h1>
20
<p>
21
Edit <code>src/App.jsx</code> and save to test <code>HMR</code>
22
</p>
23
</div>
24
<button
25
type="button"
26
className="counter"
27
onClick={() => setCount((count) => count + 1)}
28
>
29
Count is {count}
30
</button>
31
</section>
32
33
<div className="ticks"></div>
34
35
<section id="next-steps">
36
<div id="docs">
37
<svg className="icon" role="presentation" aria-hidden="true">
38
<use href="/icons.svg#documentation-icon"></use>
39
</svg>
40
<h2>Documentation</h2>
41
<p>Your questions, answered</p>
42
<ul>
43
<li>
44
<a href="https://vite.dev/" target="_blank">
45
<img className="logo" src={viteLogo} alt="" />
46
Explore Vite
47
</a>
48
</li>
49
<li>
50
<a href="https://react.dev/" target="_blank">
51
<img className="button-icon" src={reactLogo} alt="" />
52
Learn more
53
</a>
54
</li>
55
</ul>
56
</div>
57
<div id="social">
58
<svg className="icon" role="presentation" aria-hidden="true">
59
<use href="/icons.svg#social-icon"></use>
60
</svg>
61
<h2>Connect with us</h2>
62
<p>Join the Vite community</p>
63
<ul>
64
<li>
65
<a href="https://github.com/vitejs/vite" target="_blank">
66
<svg
67
className="button-icon"
68
role="presentation"
69
aria-hidden="true"
70
>
71
<use href="/icons.svg#github-icon"></use>
72
</svg>
73
GitHub
74
</a>
75
</li>
76
<li>
77
<a href="https://chat.vite.dev/" target="_blank">
78
<svg
79
className="button-icon"
80
role="presentation"
81
aria-hidden="true"
82
>
83
<use href="/icons.svg#discord-icon"></use>
84
</svg>
85
Discord
86
</a>
87
</li>
88
<li>
89
<a href="https://x.com/vite_js" target="_blank">
90
<svg
91
className="button-icon"
92
role="presentation"
93
aria-hidden="true"
94
>
95
<use href="/icons.svg#x-icon"></use>
96
</svg>
97
X.com
98
</a>
99
</li>
100
<li>
101
<a href="https://bsky.app/profile/vite.dev" target="_blank">
102
<svg
103
className="button-icon"
104
role="presentation"
105
aria-hidden="true"
106
>
107
<use href="/icons.svg#bluesky-icon"></use>
108
</svg>
109
Bluesky
110
</a>
111
</li>
112
</ul>
113
</div>
114
</section>
115
116
<div className="ticks"></div>
117
<section id="spacer"></section>
118
</>
119
)
1
function App () {
2
return <h1>Hello World</h1>
120
3
}
121
4
122
5
export default App
src/main.jsx
1
1
import { StrictMode } from 'react'
2
2
import { createRoot } from 'react-dom/client'
3
import './index.css'
4
3
import App from './App.jsx'
5
4
6
5
createRoot(document.getElementById('root')).render(

削除したファイル(favicon.svg)の読み込みを消し、lang を ja にします。

index.html
1
1
<!doctype html>
2
<html lang="en">
2
<html lang="ja">
3
3
<head>
4
4
<meta charset="UTF-8" />
5
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
6
5
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
6
<title>my-react-app</title>
8
7
</head>

開発用のサーバーを起動して動作確認をします。

Hello World と表示されていれば成功です。

この節では、React のプロジェクトの作成方法について説明しました。 空のプロジェクトができたのでここから基礎を学んでいきましょう。

次の節では状態管理を学びます。

この節を終えたときのディレクトリ

Section titled “この節を終えたときのディレクトリ”
01-react-basic-01-setup-project
function App () {
return <h1>Hello World</h1>
}
export default App
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import App from './App.jsx'
createRoot(document.getElementById('root')).render(
<StrictMode>
<App />
</StrictMode>,
)
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
node_modules
dist
dist-ssr
*.local
# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
{
"$schema": "./node_modules/oxlint/configuration_schema.json",
"plugins": ["react", "oxc"],
"rules": {
"react/rules-of-hooks": "error",
"react/only-export-components": ["warn", { "allowConstantExport": true }]
}
}
<!doctype html>
<html lang="ja">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>my-react-app</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
</body>
</html>
(npm install が書き出すファイルです。長いので、ここでは中身を省いています。ZIP には入っています)
{
"name": "my-react-app",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"lint": "oxlint",
"preview": "vite preview"
},
"dependencies": {
"react": "^19.3.1",
"react-dom": "^19.3.1"
},
"devDependencies": {
"@types/react": "^19.3.0",
"@types/react-dom": "^19.3.0",
"@vitejs/plugin-react": "^6.3.0",
"oxlint": "^1.94.0",
"vite": "^8.4.0"
}
}
import react from '@vitejs/plugin-react'
import { defineConfig } from 'vite'
// https://vite.dev/config/
export default defineConfig({
plugins: [react()],
})
この節を終えたときのファイル