mirror of
				https://github.com/khairul169/code-share.git
				synced 2025-11-04 05:31:08 +07:00 
			
		
		
		
	
		
			
				
	
	
		
			35 lines
		
	
	
		
			756 B
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			35 lines
		
	
	
		
			756 B
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
import React, { useEffect, useState } from "react";
 | 
						|
import type { ReactNode } from "react";
 | 
						|
 | 
						|
type ClientOnlyProps = {
 | 
						|
  children: ReactNode;
 | 
						|
  fallback?: ReactNode | null;
 | 
						|
};
 | 
						|
 | 
						|
const ClientOnly = ({ children, fallback }: ClientOnlyProps) => {
 | 
						|
  const [isMounted, setMounted] = useState(false);
 | 
						|
 | 
						|
  useEffect(() => {
 | 
						|
    setMounted(true);
 | 
						|
  }, []);
 | 
						|
 | 
						|
  if (typeof window === "undefined") {
 | 
						|
    return fallback;
 | 
						|
  }
 | 
						|
 | 
						|
  return isMounted ? children : fallback;
 | 
						|
};
 | 
						|
 | 
						|
export const withClientOnly = <T extends unknown>(
 | 
						|
  Component: React.ComponentType<T>,
 | 
						|
  fallback?: ReactNode | null
 | 
						|
): React.ComponentType<T> => {
 | 
						|
  return (props: any) => (
 | 
						|
    <ClientOnly fallback={fallback}>
 | 
						|
      <Component {...props} />
 | 
						|
    </ClientOnly>
 | 
						|
  );
 | 
						|
};
 | 
						|
 | 
						|
export default ClientOnly;
 |