Client Only
Renders its content only in the browser, after the page has loaded. The server shows a fallback instead.
Usage
import { ClientOnly, Skeleton } from '@leverui/components';
<ClientOnly>
<SiteThemeToggle />
</ClientOnly>
Examples
Fallback
Use the fallback prop to render a loading state while the client-side content is being prepared.
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:
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)
Direct children that use browser APIs may cause errors on the server. Pass a function to be safe.
/* This may cause server-side errors */
<ClientOnly fallback={<Skeleton />}>
<ComponentThatUsesWindow />
</ClientOnly>
/* This is safe */
<ClientOnly fallback={<Skeleton />}>
{() => <ComponentThatUsesWindow />}
</ClientOnly>
Props
| Prop | Default | Type | Description |
|---|---|---|---|
fallback | — | JSX.Element | Shown until mounted in the browser. |
children | — | JSX.Element | (() => JSX.Element) | Shown once mounted. |
Adds no wrapper element, accessibility roles, or Lever UI styles. Check that labels and keyboard focus still work when the content replaces the fallback. Keep the fallback useful with JavaScript turned off.