1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Next.js の使い方 (Reactハンズオンラーニング 第2版 のサンプル)

Last updated at Posted at 2022-05-23

Next.js は React のサーバーサイドレンダリングのためのツールです。

Reactハンズオンラーニング 第2版 の 12.3 のサンプルを動かして見ました。
Node.js のバージョンは次の通りです。

$ node --version
v18.4.0

プロジェクトの作成

mkdir proj02
cd proj02
npm init -y
npm install --save react react-dom next
mkdir pages

この時点でのフォルダーの状況

$ tree -I node_modules
.
├── package-lock.json
├── package.json
└── pages

package.json の改造

package.json
{
  "name": "proj01",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
   "dev": "next",
    "build": "next build",
    "start": "next start"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "next": "^12.2.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0"
  }
}

コードの作成

pages/index.js
import Layout from "./Layout"

export default function Index() {
  return (
    <Layout>
      <div>
        <h1>Hello everyone!</h1>
	<p>May/23/2022 PM 14:16</p>
      </div>
    </Layout>
  )
}
pages/Layout.js
export default function Layout(props) {
  return (
    <div>
{props.children}
    </div>
  )
}

この時点でのフォルダーの状況

$ tree -I node_modules/
.
├── package-lock.json
├── package.json
└── pages
    ├── Layout.js
    └── index.js

サーバーの起動

npm run dev

ブラウザーで
http://localhost:3000
にアクセス
image.png

使ったバージョン

$ npm list --depth=0

├── next@12.2.0
├── react-dom@18.2.0
└── react@18.2.0
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?