Getting Started with Next.js
Install the package
First, create your application.
Execute one of the following commands in your terminal.
pnpm create next-app my-app --typescript
Yamada UI allows you to use most components and hooks by simply installing @yamada-ui/react
.
pnpm add @yamada-ui/react
If you want to use tables, calendars, carousels, etc., you need to install them separately.
pnpm add @yamada-ui/table
Package | Description | |
---|---|---|
@yamada-ui/table | Provides convenient table components using @tanstack/react-table. | |
@yamada-ui/calendar | Provides convenient calendar and date picker components using dayjs. | |
@yamada-ui/carousel | Provides convenient carousel components using embla-carousel-react. | |
@yamada-ui/dropzone | Provides convenient dropzone components using react-dropzone. | |
@yamada-ui/charts | Provides convenient chart components using recharts. | |
@yamada-ui/markdown | Provides convenient markdown components using react-markdown and react-syntax-highlighter. | |
@yamada-ui/fontawesome | Provides components for conveniently using Font Awesome. |
For individual component and hook installation to improve performance, please check here.
If you only want to use the Yamada UI style system, please check here.
Add UIProvider
After installing Yamada UI, add UIProvider
.
_app.tsx
import type { AppProps } from "next/app"import { UIProvider } from "@yamada-ui/react"export default function App({ Component, pageProps }: AppProps) {return (<UIProvider><Component {...pageProps} /></UIProvider>)}
The Default Theme is included within UIProvider
.
If you want to customize the theme or config, please check Customize Theme and Customize Config.
Add ColorModeScript
To make the color mode work properly, you need to add ColorModeScript
inside the body
.
This is because the color mode is implemented using localStorage
or cookies
, and it needs to synchronize correctly at page load.
_document.tsx
import { Html, Head, Main, NextScript } from "next/document"import { ColorModeScript, defaultConfig } from "@yamada-ui/react"export default function Document() {return (<Html lang="en"><Head /><body><ColorModeScript initialColorMode={defaultConfig.initialColorMode} /><Main /><NextScript /></body></Html>)}
For props
' initialColorMode
, please pass the same initialColorMode
as your config. If you have your own config, pass its initialColorMode
.
Add colorModeManager
For sites rendered on the server side, such as Next.js, you may want to include the color mode in the request to avoid changes during hydration.
If you are not using server-side rendering, you do not need to follow these steps. Yamada UI uses localStorage
by default.
Prepare getServerSideProps
To standardize getServerSideProps
across multiple pages, define getServerSideCommonProps
.
import { GetServerSidePropsContext } from "next"export const getServerSideCommonProps = ({req,}: GetServerSidePropsContext) => {return {props: {cookies: req.headers.cookie ?? "",},}}
Set cookies
in colorModeManager
Set ssr
and cookies
in createColorModeManager
.
_app.tsx
import type { AppProps } from "next/app"import { UIProvider, createColorModeManager } from "@yamada-ui/react"export default function App({ Component, pageProps }: AppProps) {const { cookies } = pagePropsconst colorModeManager = createColorModeManager("ssr", cookies)return (<UIProvider colorModeManager={colorModeManager}><Component {...pageProps} /></UIProvider>)}
Add getServerSideProps
Add the previously created getServerSideCommonProps
to each page.
index.tsx
import { getServerSideCommonProps } from "../get-server-side-props.ts"import { Button } from "@yamada-ui/react"export default function Index() {return <Button>Click me!</Button>}export const getServerSideProps = getServerSideCommonProps
Use Components
After adding UIProvider
, you can call components within your application.
index.tsx
import { Button } from "@yamada-ui/react"export default function Home() {return <Button>Click me!</Button>}
Edit this page on GitHub