Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setTheme does not update DOM (next 13) #238

Closed
Yihwan opened this issue Nov 26, 2023 · 1 comment
Closed

setTheme does not update DOM (next 13) #238

Yihwan opened this issue Nov 26, 2023 · 1 comment

Comments

@Yihwan
Copy link

Yihwan commented Nov 26, 2023

  • Clicking "Dark Mode" does not activate dark mode, apparently because DOM is not updating.
  • theme from useTheme receives current theme as expected.
  • Manually updating DOM changes theme as expected.
  • Minimal repro: https://github.com/Yihwan/next-themes-minimal-repro
import type { Metadata } from 'next';
import localFont from 'next/font/local';

import { ThemeProvider } from '@/components/ThemeProvider';

// ...

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang='en' suppressHydrationWarning>
      <body
        className={cn(
          'bg-background font-sans antialiased',
          interFont.variable,
        )}
      >
        <LayoutHeader />

        <ThemeProvider
          attribute='class'
          defaultTheme='system'
          disableTransitionOnChange
        >
          <main className='p-4'>{children}</main>
        </ThemeProvider>
      </body>
    </html>
  );
}
// ThemeProvider
'use client';

import { ThemeProvider as NextThemesProvider } from 'next-themes';
import { type ThemeProviderProps } from 'next-themes/dist/types';
import * as React from 'react';

export function ThemeProvider({ children, ...props }: ThemeProviderProps) {
  return <NextThemesProvider {...props}>{children}</NextThemesProvider>;
}
// LayoutHeader 
'use client';

import { useTheme } from 'next-themes';
import * as React from 'react';

export default function LayoutHeader() {
  const { theme, setTheme } = useTheme();

  return (
    <header>
      <div>
        The current theme is: {theme}
        <button onClick={() => setTheme('light')}>Light Mode</button>
        <button onClick={() => setTheme('dark')}>Dark Mode</button>
      </div>
    </header>
  );
}
@Yihwan
Copy link
Author

Yihwan commented Nov 26, 2023

just realized i didn't put the header in the provider 🤦

@Yihwan Yihwan closed this as completed Nov 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant