useColorScheme
React and React Native
A React Native hook that returns the system colour scheme as light or dark, never null.
The system colour scheme, always "light" or "dark". React Native's own hook can return null before the platform reports a value; this one falls back to light so styles never see a missing scheme. It re-renders when the person switches appearance. Works in React and in React Native with no extra packages.
Installation
CLI
npx @modus-ui/cli add use-color-schemeManual
- Copy the code from the code tab, using the React or React Native switch for your project.
- Create a file at
components/ui/use-color-scheme.ts. - Paste the code.
The CLI installs the React Native version in an Expo or React Native app and the React version everywhere else. In a project that targets both, keep the React file use-color-scheme.ts and the React Native file use-color-scheme.native.ts side by side and import ./use-color-scheme; Metro picks the .native file on its own.
Usage
tsx
API
| Part | Type | Description |
|---|---|---|
| Returns | "light" | "dark" | The scheme in effect, light when none is set. |
Comments