Skip to main content

useTheme

React hook provided by next-themes for accessing current theme state and controlling theme changes. Used throughout the tweakcn Theme Picker system.
This hook is provided by the next-themes library and re-exported from next-themes. It must be used within a ThemeProvider.

Hook Signature

Return Values

string | undefined
The current active theme value (e.g., "catppuccin-dark", "vercel-light").Returns undefined during SSR or before hydration. Always check for undefined or use the mounted pattern.
(theme: string) => void
Function to change the current theme. Accepts any value from the themes array.
string[]
Array of all available theme values configured in ThemeProvider.For tweakcn Theme Picker, this contains all 90 theme variants (45 themes × 2 modes).
'light' | 'dark' | undefined
The user’s OS-level theme preference ("light" or "dark").Not used in tweakcn Theme Picker since enableSystem={false} in the provider.
string | undefined
The actual theme being used after resolving system preferences.In tweakcn, this is the same as theme since system theme is disabled.
string | undefined
Theme value if forced by parent component configuration.Not used in tweakcn Theme Picker.

Basic Usage

Simple Theme Access

Theme Switching

Advanced Usage

Hydration-Safe Theme Access

Prevent hydration mismatches by checking if the component has mounted:

Parsing Theme Name and Mode

Extract color theme and mode from the theme string:

Theme Validation

Ensure theme values are valid before setting:

Get Current Theme Configuration

Lookup the full theme config object:

React to Theme Changes

Run side effects when theme changes:

Programmatic Theme Selection

Cycle through themes or implement custom logic:

Common Patterns

Theme Persistence

Themes are automatically persisted to localStorage by next-themes:

SSR Considerations

The theme value is undefined during SSR. Always handle this case:

Type Safety

Create typed helpers for better autocomplete:

Requirements

This hook MUST be used inside a ThemeProvider. Using it outside will throw an error.

Full Example

Complete implementation using all major features:

See Also