File size: 992 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
import React, { ComponentType, useEffect, useState } from 'react'

/**
 * A higher-order component that prevents a component from rendering server-side
 *
 * @template P - The props object of the wrapped component
 * @param {React.ComponentType<P>} Component - The component to be wrapped
 * @returns {React.FC<P>} A new component that renders the given component client-side only
 */
// eslint-disable-next-line max-len
const withNoSSR = <P extends Record<string, unknown>>(Component: ComponentType<P>) => (props: P) => {
  const [isClient, setIsClient] = useState(false)

  /**
   * Sets isClient to true when the component is mounted on the client side
   */
  useEffect(() => {
    setIsClient(true)
  }, [])

  // Renders nothing if the component is not mounted on the client side
  if (!isClient) return null

  // Renders the wrapped component with the given props if it's mounted on the client side
  return <Component {...props} />
}

export {
  withNoSSR as default,
  withNoSSR,
}