Overview
TheuseColorScheme hook detects the user’s color scheme preference and returns either 'light', 'dark', or null. On web platforms, it handles hydration to support static rendering by defaulting to 'light' until the client-side hydration is complete.
Import
Usage
Basic Usage
With Theme Provider
Fromsrc/app/_layout.tsx:
Conditional Styling
Fromsrc/components/ui/collapsible.tsx:
Return Value
Returns the user’s color scheme preference:'light'- Light mode is preferred'dark'- Dark mode is preferrednull- No preference detected (native platforms only)
'light' during server-side rendering and updates to the actual preference after client-side hydration.
Platform Behavior
Native (iOS/Android)
Directly uses React Native’s built-inuseColorScheme hook, which detects the system color scheme preference.
Web
Implements special handling for static rendering:- Returns
'light'initially (before hydration) - After hydration completes, returns the actual color scheme from React Native’s hook
- This ensures consistent rendering between server and client
Type Definition
Best Practices
Use Null Coalescing
Since the hook can returnnull, use the null coalescing operator to provide a default:
Combine with Theme Constants
Use with your theme constants for consistent styling:Consider Using useThemeColor
For color-specific use cases, consider using theuseThemeColor hook instead, which provides automatic color resolution based on the theme.
Related Hooks
useThemeColor- Get theme-aware colors automatically