# useTheme (/docs/hooks/use-theme)





`useTheme` manages a persisted `"light" | "dark" | "system"` choice and keeps `data-theme` on `<html>` in sync with it. Reach for it when you build a theme switch. Setting the theme up for an app (the blocking script that prevents a flash of the wrong theme on first load) is covered in [Theming](/docs/theming).

```tsx
import { useTheme } from '@42-central/ui-react';

const { mode, resolvedTheme, setMode } = useTheme();

<button onClick={() => setMode(resolvedTheme === 'dark' ? 'light' : 'dark')}>
  <SunIcon className="dark:hidden" />
  <MoonIcon className="hidden dark:block" />
</button>
```

## Usage [#usage]

This switch drives this site's theme. The choice is saved in `localStorage`, so it follows you to another tab and survives a reload. `system` follows your OS setting live.

<Preview code="&#x22;const { mode, resolvedTheme, setMode } = useTheme();\n\n<SegmentGroup\n  data={['light', 'dark', 'system']}\n  value={mode}\n  onChange={(next) => next && setMode(next)}\n/>\n<p>mode: {mode} · resolvedTheme: {resolvedTheme}</p>&#x22;">
  <ThemeDemo />
</Preview>

`setMode` also takes an updater resolved against the current mode, like `useState`'s setter: `setMode((m) => NEXT[m])` cycles through the three.

<Callout type="warn" title="Don't render resolvedTheme">
  A server can't know the visitor's preference: `resolvedTheme` is `"light"` in the server HTML and on the first client render, and `mode` is `defaultColorScheme`, so anything rendered from them shows the wrong theme until hydration. Read them for logic (what a click switches to), and paint the visible state with the `dark:` variant, which keys off the same `data-theme` attribute and is right from the first frame. The demo above renders its readout only after hydration for that reason.
</Callout>

## API [#api]

<TypeTable
  type="{
  storageKey: {
    type: 'string',
    default: &#x22;'ui-theme'&#x22;,
    description: 'The localStorage key. Must match <ThemeScript storageKey> or the pasted init script.',
  },
  defaultColorScheme: {
    type: &#x22;'light' | 'dark' | 'system'&#x22;,
    default: &#x22;'system'&#x22;,
    description: 'Used when nothing is stored yet, or the stored value is invalid.',
  },
  forceColorScheme: {
    type: &#x22;'light' | 'dark'&#x22;,
    description: 'Pins mode and resolvedTheme, and makes setMode a no-op (no storage write, no DOM write). Pass it to <ThemeScript> too.',
  },
  keepTransitions: {
    type: 'boolean',
    default: 'false',
    description: 'Transitions are suppressed for a frame around every applied change unless this is true.',
  },
  attribute: {
    type: &#x22;'data-theme' | 'class' | ('data-theme' | 'class')[]&#x22;,
    default: &#x22;'data-theme'&#x22;,
    description: 'Which DOM mechanism to keep in sync. Add class for CSS that reads a .dark class. Must match <ThemeScript attribute>.',
  },
}"
/>

<TypeTable
  type="{
  mode: { type: &#x22;'light' | 'dark' | 'system'&#x22;, required: true, description: &#x22;The user's persisted choice, 'system' included.&#x22; },
  resolvedTheme: { type: &#x22;'light' | 'dark'&#x22;, required: true, description: &#x22;The applied value, 'system' already resolved. Read from the DOM, so it reflects whatever the init script or setMode wrote.&#x22; },
  setMode: { type: &#x22;(mode: ThemeMode | ((prev: ThemeMode) => ThemeMode)) => void&#x22;, required: true, description: 'Persists the choice and applies the attribute in the same call.' },
}"
/>

Every option is detailed, with the Next.js and Vite setups, in [Theming's API reference](/docs/theming#api-reference).
