Skip to main content
The 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 through next-themes, which is automatically installed with the theme system:

Basic Usage

Return Values

The useTheme 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 to localStorage by next-themes. The stored value persists across browser sessions:

Hydration Handling

The theme value is undefined during server-side rendering and initial client render. Always handle this case:
Or use optional chaining:

Theme Naming Convention

All tweakcn themes follow the pattern {name}-{mode}:
When building custom selectors, always include both the theme name and mode:

Using with Theme Config

Combine with themes-config.ts for rich theme metadata:

Next.js Server Components

The useTheme hook only works in Client Components. Mark your component with "use client":
If you need theme data in Server Components, access it via cookies (advanced):
Server-side theme access is read-only. Use client components with useTheme for interactive theme switching.

ThemeSwitcher

Pre-built theme selector component

Custom Selector

Build your own theme selector