Overview
TheuseThemeColor hook returns the appropriate color value based on the current color scheme (light or dark mode). It allows you to specify custom colors or fall back to the app’s default theme colors.
Usage
Parameters
An object containing optional custom color values for light and dark modes:If you don’t want to override the default theme colors, pass an empty object
{}.The name of the color from the theme’s color palette. Must exist in both
Colors.light and Colors.dark.Common color names include:backgroundtexttinticontabIconDefaulttabIconSelected
Return Value
The resolved color value as a string. The hook determines which color to return based on this priority:
- Custom color from props: If
props.lightorprops.darkis provided (depending on current theme) - Default theme color: Falls back to
Colors[theme][colorName]from the theme constants
Type Signature
How It Works
- The hook calls
useColorScheme()to detect the current color scheme (‘light’ or ‘dark’) - Defaults to ‘light’ if color scheme cannot be determined
- Checks if a custom color is provided in
props[theme] - If custom color exists, returns it
- Otherwise, returns the default color from
Colors[theme][colorName]
Examples
Using Default Theme Colors
Using Custom Colors
Partial Override
Creating Themed Components
Theme Support
The hook automatically responds to system theme changes. When the user switches between light and dark mode:- The
useColorScheme()hook detects the change - Components using
useThemeColorautomatically re-render with the new colors - No manual intervention required
Dependencies
@/src/constants/Colors- Theme color constants@/src/hooks/useColorScheme- Color scheme detection hook
Related
- Expo Color Schemes Documentation
Colorsconstant in@/src/constants/ColorsuseColorSchemehook for detecting current theme
