--- title: ClientOnly description: "`ClientOnly`は、クライアントサイドでのみ子要素をレンダリングするコンポーネントです。" links: - source: https://github.com/yamada-ui/yamada-ui/tree/main/packages/react/src/components/client-only - storybook: https://yamada-ui.github.io/yamada-ui?path=/story/components-clientonly--basic --- ```tsx } /> ``` ## 使い方 ```tsx import { ClientOnly } from "@yamada-ui/react" ``` ```tsx import { ClientOnly } from "@/components/ui" ``` ```tsx import { ClientOnly } from "@workspaces/ui" ``` ```tsx ``` ### フォールバック 子要素の準備中に読み込み状態をレンダリングする場合は、`fallback`プロパティを使用します。 ```tsx }> } /> ``` ### レンダリングプロップ コンポーネントが`window`, `document`, `localStorage`などのブラウザ専用APIにアクセスする場合は、レンダリングプロパティパターンを使用します。 このパターンにより、ブラウザAPIにアクセスするコンポーネントがクライアント側でのみ評価され、ハイドレーションの不一致やサーバー側のエラーが防止されます。\ また、サーバー側では必要のない重いコンポーネントのレンダリングにも使用できます。 ```tsx }> {() => ( Current URL: {window.location.href} Screen width: {window.innerWidth}px )} ``` :::warning コンポーネントを直接渡すこともできますが、ブラウザAPIにアクセスするコンポーネントの場合は注意が必要です。 ::: ```tsx /* サーバー側でエラーを引き起こす可能性があります */ }> /* 安全です */ }> {() => } ``` ## Props | Prop | Default | Type | Description | | ---------- | ------- | --------------------- | -------------------------------------------------------------------------------------------------------------- | | `children` | - | `ReactNodeOrFunction` | The content to render on the client side. **Note:** Use the function pattern when accessing browser-only APIs. | | `fallback` | - | `ReactNode` | The fallback content to render while the component is mounting on the client side. |