import React from "react"; /** * Component that only renders its children on the client side * @param children - React children to render client-side * @param fallback - Optional fallback to render during SSR */ export function ClientOnly({ children, fallback = null, }: { children: React.ReactNode; fallback?: React.ReactNode; }) { const [hasMounted, setHasMounted] = React.useState(false); React.useEffect(() => { setHasMounted(true); }, []); if (!hasMounted) { return <>{fallback}>; } return <>{children}>; } /** * Creates a React component that only renders on the client side after hydration * @param Component - The component to render client-side * @param fallback - Optional fallback to render server-side * @returns A component that avoids hydration mismatches */ export function createClientOnlyComponent
>( Component: React.ComponentType
, fallback?: React.ComponentType
, ): React.ComponentType
{ return function ClientOnlyWrapper(props: P) { const [isClient, setIsClient] = React.useState(false); React.useEffect(() => { setIsClient(true); }, []); if (!isClient) { return fallback ? React.createElement(fallback, props) : null; } return React.createElement(Component, props); } as React.ComponentType
; }