useTheme hook from next-themes provides programmatic access to the current theme and methods to change it. Use this hook to build custom theme selectors or implement theme-aware components.
Installation
The hook is available throughnext-themes, which is automatically installed with the theme system:
Basic Usage
Return Values
TheuseTheme hook returns an object with the following properties:
string | undefined
The current theme value (e.g.,
"catppuccin-dark", "vercel-light"). Will be undefined during initial render before hydration.(theme: string) => void
Function to change the current theme. Pass any valid theme value like
"cyberpunk-dark" or "nature-light".string[]
Array of all available theme values from the
ThemeProvider. Contains all installed themes in both light and dark variants.'light' | 'dark' | undefined
The system’s color scheme preference. Always
undefined in tweakcn theme system because enableSystem={false} in the provider.string | undefined
The actual theme being displayed. Same as
theme in the tweakcn system.Examples
Get Current Theme
Switch Theme
Toggle Light/Dark Mode
Preserve the color theme while toggling between light and dark:List All Available Themes
Simple Theme Selector
Create a basic dropdown selector:Theme-Aware Component
Conditionally render based on the current theme:Parse Theme Components
Extract theme name and mode from the theme string:Switch Color Theme (Preserve Mode)
Change the color theme while keeping the current light/dark mode:Theme Persistence
Theme selection is automatically persisted tolocalStorage by next-themes. The stored value persists across browser sessions:
Hydration Handling
Thetheme value is undefined during server-side rendering and initial client render. Always handle this case:
Theme Naming Convention
All tweakcn themes follow the pattern{name}-{mode}:
Using with Theme Config
Combine withthemes-config.ts for rich theme metadata:
Next.js Server Components
TheuseTheme hook only works in Client Components. Mark your component with "use client":
Server-side theme access is read-only. Use client components with
useTheme for interactive theme switching.Related
ThemeSwitcher
Pre-built theme selector component
Custom Selector
Build your own theme selector