Newsletter
Join the Community
Subscribe to our newsletter for the latest news and updates
React Hook that detects the user's system color scheme (light/dark) and updates live whenever it changes, with SSR support.
use-system-theme is a React Hook that returns the user's preferred color scheme ('light' or 'dark') derived from the prefers-color-scheme media query and automatically updates when the system theme changes.
use-system-theme is a small open-source npm package created by xcv58, available under the MIT license. It provides a single React Hook, useSystemTheme, which takes an optional boolean argument isSSR and returns a string of type SystemTheme (either 'light' or 'dark'). The hook requires React version greater than 16.8.0 because it relies on React Hooks. It can be used in any React application, from single-page apps to server-side rendered ones.
prefers-color-scheme media query to determine whether the OS is in light or dark mode.true as the isSSR parameter makes the hook return 'light' during server rendering, then update to 'dark' on the client if needed, preventing hydration mismatches.declare const useSystemTheme: (isSSR?: boolean) => SystemTheme signature in the README.prefers-color-scheme, so the hook works without polyfills.hydrate constraint.localStorage.isSSR flag to ensure the initial render matches the server output, then update on the client.The hook uses the browser's matchMedia API to listen to the (prefers-color-scheme: dark) media query. It reads the current value and registers a change listener so the returned state stays in sync with the OS theme. When isSSR is true, the initial state is forced to 'light' because the server cannot know the client's theme; after hydration, the listener updates it to the actual value. This approach means SSR apps may briefly flash a light theme for dark-mode users, an unavoidable consequence of React hydration.
'light' even on dark-mode devices (the README acknowledges this as intrinsic to React hydration).The package is free and open source under the MIT license. There are no paid tiers or premium features.
You can install it with yarn add use-system-theme or npm install --save use-system-theme. Then import it as import useSystemTheme from 'use-system-theme'.
Yes, but you must call it with true as the argument: useSystemTheme(true). This makes the initial returned value 'light' to match server output. On the client, the hook updates to the real theme once hydrated.
React version greater than 16.8.0 is required because the package uses React Hooks. Earlier versions do not support hooks.
It returns a SystemTheme string, which is either 'light' or 'dark'. You can use this value to conditionally apply styles or render different content.
Yes, it's released under the MIT license, so you can use it in personal and commercial projects without cost.
