[Dúvida] Quando usar isMounted no ReactJs/Nextjs?
Estou estudando React Js e estava vendo um projeto open source onde vi um custom hook chamado useMounted e não consegui entender exatamente para que e quando ele é usado.
import * as React from "react"
export function useMounted() {
const [mounted, setMounted] = React.useState(false)
React.useEffect(() => {
setMounted(true)
}, [])
return mounted
}
Aqui está a parte onde foi usado esse hook:
export function ThemeCustomizer() {
const [config, setConfig] = useConfig()
const { resolvedTheme: mode } = useTheme()
const [mounted, setMounted] = React.useState(false)
React.useEffect(() => {
setMounted(true)
}, [])
return (
<div className="flex items-center space-x-2">
<Drawer.Root>
<DrawerTrigger asChild>
<Button variant="outline" className="md:hidden">
<Paintbrush className="mr-2 h-4 w-4" />
Customize
</Button>
</DrawerTrigger>
<DrawerContent className="h-[85%] p-6 pt-10">
<Customizer />
</DrawerContent>
</Drawer.Root>
<div className="hidden md:flex">
<div className="mr-2 hidden items-center space-x-0.5 lg:flex">
{mounted ? (
<>
{["zinc", "rose", "blue", "green", "orange"].map((color) => {
const theme = themes.find((theme) => theme.name === color)
const isActive = config.theme === color
if (!theme) {
return null
}
return (
<Tooltip key={theme.name}>
<TooltipTrigger asChild>
<button
onClick={() =>
setConfig({
...config,
theme: theme.name,
})
}
className={cn(
"flex h-9 w-9 items-center justify-center rounded-full border-2 text-xs",
isActive
? "border-[--theme-primary]"
: "border-transparent"
)}
style={
{
"--theme-primary": `hsl(${
theme?.activeColor[
mode === "dark" ? "dark" : "light"
]
})`,
} as React.CSSProperties
}
>
<span
className={cn(
"flex h-6 w-6 items-center justify-center rounded-full bg-[--theme-primary]"
)}
>
{isActive && (
<CheckIcon className="h-4 w-4 text-white" />
)}
</span>
<span className="sr-only">{theme.label}</span>
</button>
</TooltipTrigger>
<TooltipContent
align="center"
className="rounded-[0.5rem] bg-zinc-900 text-zinc-50"
>
{theme.label}
</TooltipContent>
</Tooltip>
)
})}
</>
) : (
<div className="mr-1 flex items-center space-x-3">
<Skeleton className="h-6 w-6 rounded-full" />
<Skeleton className="h-6 w-6 rounded-full" />
<Skeleton className="h-6 w-6 rounded-full" />
<Skeleton className="h-6 w-6 rounded-full" />
<Skeleton className="h-6 w-6 rounded-full" />
</div>
)}
</div>
<Popover>
<PopoverTrigger asChild>
<Button variant="outline">
<Paintbrush className="mr-2 h-4 w-4" />
Customize
</Button>
</PopoverTrigger>
<PopoverContent
align="end"
className="z-40 w-[340px] rounded-[0.5rem] bg-white p-6 dark:bg-zinc-950"
>
<Customizer />
</PopoverContent>
</Popover>
</div>
<CopyCodeButton />
</div>
)
}
Código completo: Github - Shadcn/ui
Alguem poderia me explicar?