next-themes
kütüphanesi kullanılır.npm install next-themes
ThemeProvider
, uygulamanın root seviyesinde (layout.jsx
veya _app.js
) sarılır.suppressHydrationWarning
prop’u, sunucu ve istemci tarafı HTML uyuşmazlıklarını gizler, özellikle tema başlangıcında.// app/layout.jsx
import { ThemeProvider } from 'next-themes'
export default function Layout({ children }) {
return (
<html suppressHydrationWarning>
<head />
<body>
<ThemeProvider>{children}</ThemeProvider>
</body>
</html>
)
}
✅ Böylece tüm sayfalar ve bileşenler, mevcut tema bilgisini useTheme hook’u ile alabilir.
useTheme
hook’u ile mevcut tema (theme
) ve tema değiştirme fonksiyonu (setTheme
) elde edilir.import { useTheme } from 'next-themes'
const ThemeChanger = () => {
const { theme, setTheme } = useTheme()
return (
<div>
Current theme: {theme}
<button onClick={() => setTheme('light')}>Light Mode</button>
<button onClick={() => setTheme('dark')}>Dark Mode</button>
</div>
)
}
theme
değeri "light"
, "dark"
veya "system"
olabilir."system"
: Kullanıcının işletim sistemi temasına uyum sağlar.