Skip to content

Commit

Permalink
fix(ui): return toggle theme component just when mounted
Browse files Browse the repository at this point in the history
Fix hydration error
  • Loading branch information
mateusfg7 committed Jun 12, 2023
1 parent 3df6194 commit 9dbe8f7
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/app/(blog)/components/header/toggle-theme.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
'use client'

import { useEffect, useState } from 'react'
import { Menu } from '@headlessui/react'
import { Moon, Sun, Desktop } from '@phosphor-icons/react'
import { useTheme } from 'next-themes'

export function ToggleTheme() {
const [isMounted, setIsMounted] = useState(false)

const { setTheme, theme: currTheme } = useTheme()

const SelectTheme = ({ theme }: { theme: 'light' | 'dark' | 'system' }) => (
Expand Down Expand Up @@ -35,6 +38,10 @@ export function ToggleTheme() {
</button>
)

useEffect(() => setIsMounted(true), [])

if (!isMounted) return <></>

return (
<Menu as="div" className="relative inline-flex items-center">
<Menu.Button>
Expand Down

0 comments on commit 9dbe8f7

Please sign in to comment.