Files
cunkebao_v3/SuperAdmin/components/ClientOnly.tsx
2025-04-10 16:52:05 +08:00

18 lines
533 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use client"
import { useState, useEffect, type ReactNode } from 'react'
/**
* ClientOnly组件
* 该组件专门用于包装那些只能在客户端渲染的内容,避免水合不匹配错误
* 例如使用了Date.now()或window对象的组件
*/
export default function ClientOnly({ children, fallback = null }: { children: ReactNode, fallback?: ReactNode }) {
const [isClient, setIsClient] = useState(false)
useEffect(() => {
setIsClient(true)
}, [])
return isClient ? <>{children}</> : <>{fallback}</>
}