File size: 1,643 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
---
title: Client Only
description: Used to render content only on the client side.
links:
source: components/client-only
---
## Usage
```jsx
import { ClientOnly, Skeleton } from "@chakra-ui/react"
```
```jsx
<ClientOnly>
<ColorModeButton />
</ClientOnly>
```
## Examples
### Fallback
Use the `fallback` prop to render a loading state while the client-side content
is being prepared.
```jsx
<ClientOnly fallback={<Skeleton boxSize="8" />}>
<ColorModeButton />
</ClientOnly>
```
### Render Prop (Recommended)
When your component accesses browser-only APIs (like `window`, `document`, or
`localStorage`), use the render prop pattern to prevent server-side rendering
issues:
```jsx
<ClientOnly fallback={<Skeleton />}>
{() => (
<div>
Current URL: {window.location.href}
Screen width: {window.innerWidth}px
</div>
)}
</ClientOnly>
```
This pattern ensures that components accessing browser APIs are only evaluated
on the client side, preventing hydration mismatches and server-side errors.
> It can also be used for rendering heavy components that are not needed on the
> server side.
### Direct Children (Use with Caution)
While you can pass components directly as children, be careful with components
that access browser APIs:
```tsx
/* ❌ This may cause server-side errors */
<ClientOnly fallback={<Skeleton />}>
<ComponentThatUsesWindow />
</ClientOnly>
/* ✅ This is safe */
<ClientOnly fallback={<Skeleton />}>
{() => <ComponentThatUsesWindow />}
</ClientOnly>
```
## Props
These props can be passed to the `ClientOnly` component.
<PropTable component="ClientOnly" part="ClientOnly" />
|