---
title: Next.js (Pages)
description: "Next.jsの`pages`ディレクトリにYamada UIをインストールして使用するためのガイド。"
---
## インストール
### アプリケーションを作成する
Next.jsのアプリケーションを作成します。
```bash
pnpm create next-app my-app --typescript
```
```bash
npx create-next-app my-app --typescript
```
```bash
yarn create next-app my-app --typescript
```
```bash
bun create next-app my-app --typescript
```
### セットアップする
コマンドを実行すると、プロジェクトに必要なファイルやフォルダが作成されます。
```bash
pnpm dlx @yamada-ui/cli init
```
```bash
npx @yamada-ui/cli init
```
```bash
yarn dlx @yamada-ui/cli init
```
```bash
bunx @yamada-ui/cli init
```
### パッケージをインストールする
アプリケーションに`@workspaces/ui`をインストールします。
```bash
pnpm add "@workspaces/ui@workspace:*"
```
```bash
npm install "@workspaces/ui@workspace:*"
```
```bash
yarn add "@workspaces/ui@workspace:*"
```
```bash
bun add "@workspaces/ui@workspace:*"
```
### プロバイダーを追加する
インストール後、アプリケーションのルートに`UIProvider`を追加します。
```tsx
import { UIProvider } from "@workspaces/ui"
import type { AppProps } from "next/app"
export default function App({ Component, pageProps }: AppProps) {
return (
)
}
```
### コンポーネントを使用する
`UIProvider`を追加したら、アプリケーション内でコンポーネントを使用します。
```tsx
import { Button } from "@workspaces/ui"
export default function Home() {
return
}
```
これで、Yamada UIのセットアップは完了です!
## スクリプト
### ColorModeScript
[カラーモード](https://yamada-ui.com/docs/theming/color-mode.md)を使用する場合は、正常に動作させるために`body`に`ColorModeScript`を追加する必要があります。
理由は、カラーモードが`localStorage`や`cookies`を用いて実装されており、ページの読み込み時に同期を正しく機能させるためです。
#### スクリプトを追加する
```tsx
import { Html, Head, Main, NextScript } from "next/document"
import { ColorModeScript } from "@workspaces/ui"
export default function Document() {
return (
)
}
```
もし、[コンフィグ](https://yamada-ui.com/docs/theming/configuration/overview.md)の`defaultColorMode`を変更した場合は、`ColorModeScript`に`defaultValue`を設定します。
```tsx
import { Html, Head, Main, NextScript } from "next/document"
import { ColorModeScript } from "@workspaces/ui"
import { config } from "@workspace/theme"
export default function Document() {
return (
)
}
```
#### getServerSidePropsを追加する
複数のページで[getServerSideProps](https://nextjs.org/docs/pages/building-your-application/data-fetching/get-server-side-props)を共通化するために、`getServerSideSharedProps`を定義します。
```tsx
import { GetServerSidePropsContext } from "next"
export const getServerSideSharedProps = ({
req,
}: GetServerSidePropsContext) => {
return {
props: {
cookies: req.headers.cookie ?? "",
},
}
}
```
```tsx
import { getServerSideSharedProps } from "@/get-server-side-props"
import { Button } from "@workspaces/ui"
export const getServerSideProps = getServerSideSharedProps
export default function Home() {
return
}
```
#### プロバイダーを更新する
```tsx
import { UIProvider } from "@workspaces/ui"
import type { AppProps } from "next/app"
export default function App({ Component, pageProps }: AppProps) {
const { cookie } = pageProps
return (
)
}
```
### ThemeSchemeScript
[テーマの切り替え](https://yamada-ui.com/docs/theming/switching-themes.md)を使用する場合は、正常に動作させるために`body`に`ThemeSchemeScript`を追加する必要があります。
理由は、テーマの切り替えが`localStorage`や`cookies`を用いて実装されており、ページの読み込み時に同期を正しく機能させるためです。
#### スクリプトを追加する
```tsx
import { Html, Head, Main, NextScript } from "next/document"
import { ThemeSchemeScript } from "@workspaces/ui"
export default function Document() {
return (
)
}
```
もし、[コンフィグ](https://yamada-ui.com/docs/theming/configuration/overview.md)の`defaultThemeScheme`を変更した場合は、`ThemeSchemeScript`に`defaultValue`を設定します。
```tsx
import { Html, Head, Main, NextScript } from "next/document"
import { ThemeSchemeScript } from "@workspaces/ui"
import { config } from "@workspace/theme"
export default function Document() {
return (
)
}
```
#### getServerSidePropsを追加する
複数のページで[getServerSideProps](https://nextjs.org/docs/pages/building-your-application/data-fetching/get-server-side-props)を共通化するために、`getServerSideSharedProps`を定義します。
```tsx
import { GetServerSidePropsContext } from "next"
export const getServerSideSharedProps = ({
req,
}: GetServerSidePropsContext) => {
return {
props: {
cookies: req.headers.cookie ?? "",
},
}
}
```
```tsx
import { getServerSideSharedProps } from "@/get-server-side-props"
import { Button } from "@workspaces/ui"
export const getServerSideProps = getServerSideSharedProps
export default function Home() {
return
}
```
#### プロバイダーを更新する
```tsx
import { UIProvider } from "@workspaces/ui"
import type { AppProps } from "next/app"
export default function App({ Component, pageProps }: AppProps) {
const { cookie } = pageProps
return (
)
}
```