File size: 765 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
"use client"

import { useEffect, useState } from "react"
import { Show } from "../show"

export interface ClientOnlyProps {
  /**
   * The content to render on the client side.
   *
   * **Note:** Use the function pattern when accessing browser-only APIs.
   */
  children: React.ReactNode | (() => React.ReactNode)
  /**
   * The fallback content to render while the component is mounting on the client
   * side.
   */
  fallback?: React.ReactNode | undefined
}

export const ClientOnly = (props: ClientOnlyProps) => {
  const { children, fallback } = props
  const [hasMounted, setHasMounted] = useState(false)

  useEffect(() => {
    setHasMounted(true)
  }, [])

  return (
    <Show when={hasMounted} fallback={fallback}>
      {children}
    </Show>
  )
}