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プロジェクトの中身
Section titled “プロジェクトの中身”次のようなプロジェクトが作成されます。
./├── .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 “作られたプロジェクトの起動”まずは作成されたプロジェクトを起動してみましょう。
依存パッケージのインストール
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/ にアクセスしてみましょう。
ページが表示されれば成功です。
不要なファイルの削除
Section titled “不要なファイルの削除”最低元の構成にしていきます。
不要なファイルの削除
Section titled “不要なファイルの削除”次のファイルを削除します。
public/favicon.svgpublic/icons.svgsrc/assets/hero.pngsrc/assets/react.svgsrc/assets/vite.svgsrc/App.csssrc/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ファイルの削除に関する修正
Section titled “ファイルの削除に関する修正”削除したファイルを参照している箇所を修正します。
各ファイルを次のように修正します。
src/App.jsx を変更する
Section titled “src/App.jsx を変更する”1import { useState } from 'react'2import heroImg from './assets/hero.png'3import reactLogo from './assets/react.svg'4import viteLogo from './assets/vite.svg'5import './App.css'6
7function 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 <button25 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 Vite47 </a>48 </li>49 <li>50 <a href="https://react.dev/" target="_blank">51 <img className="button-icon" src={reactLogo} alt="" />52 Learn more53 </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 <svg67 className="button-icon"68 role="presentation"69 aria-hidden="true"70 >71 <use href="/icons.svg#github-icon"></use>72 </svg>73 GitHub74 </a>75 </li>76 <li>77 <a href="https://chat.vite.dev/" target="_blank">78 <svg79 className="button-icon"80 role="presentation"81 aria-hidden="true"82 >83 <use href="/icons.svg#discord-icon"></use>84 </svg>85 Discord86 </a>87 </li>88 <li>89 <a href="https://x.com/vite_js" target="_blank">90 <svg91 className="button-icon"92 role="presentation"93 aria-hidden="true"94 >95 <use href="/icons.svg#x-icon"></use>96 </svg>97 X.com98 </a>99 </li>100 <li>101 <a href="https://bsky.app/profile/vite.dev" target="_blank">102 <svg103 className="button-icon"104 role="presentation"105 aria-hidden="true"106 >107 <use href="/icons.svg#bluesky-icon"></use>108 </svg>109 Bluesky110 </a>111 </li>112 </ul>113 </div>114 </section>115
116 <div className="ticks"></div>117 <section id="spacer"></section>118 </>119 )1function App () {2 return <h1>Hello World</h1>1203}1214
1225export default Appsrc/main.jsx を変更する
Section titled “src/main.jsx を変更する”11import { StrictMode } from 'react'22import { createRoot } from 'react-dom/client'3import './index.css'43import App from './App.jsx'54
65createRoot(document.getElementById('root')).render(index.html を変更する
Section titled “index.html を変更する”削除したファイル(favicon.svg)の読み込みを消し、lang を ja にします。
11<!doctype html>2<html lang="en">2<html lang="ja">33 <head>44 <meta charset="UTF-8" />5 <link rel="icon" type="image/svg+xml" href="/favicon.svg" />65 <meta name="viewport" content="width=device-width, initial-scale=1.0" />76 <title>my-react-app</title>87 </head>開発用のサーバーを起動して動作確認をします。
Hello World と表示されていれば成功です。
この節では、React のプロジェクトの作成方法について説明しました。 空のプロジェクトができたのでここから基礎を学んでいきましょう。
次の節では状態管理を学びます。