useTheme
Provides access to the current theme and method to switch themes.
Usage
Important
Make sure your application is wrapped with ThemeProvider
before using
useTheme
to utilise the API provided.
Example
Building a Theme Switch Component
The useTheme
hook provides an easy way to manage theme states within your application. Below, we define a custom component that allows users to toggle between light and dark modes.
Declaring a Globally Accessible ThemeProvider
To apply the theme context across the application, wrap the entire layout with the ThemeProvider
. This ensures that all child components can access the theme state and toggle functionality.
API
useTheme
Prop | Type | Default |
---|---|---|
theme | string | light |
setTheme | (theme: string) => void | - |
ThemeProvider
Prop | Type | Default |
---|---|---|
defaultTheme | string | light |
setTheme | (theme: string) => void | - |