client-only

PreviousNext
Docs
diceuicomponent

Preview

Loading preview…
components/client-only.tsx
"use client";

import * as React from "react";

interface ClientOnlyProps {
  children: React.ReactNode;
  fallback?: React.ReactNode;
}

function ClientOnly({ children, fallback = null }: ClientOnlyProps) {
  const [mounted, setMounted] = React.useState(false);

  React.useLayoutEffect(() => {
    setMounted(true);
  }, []);

  if (!mounted) return fallback;

  return children;
}

export { ClientOnly };

Installation

npx shadcn@latest add @diceui/client-only

Usage

import { ClientOnly } from "@/components/client-only"
<ClientOnly />