useTheme
Creating and using theme values
Access the current theme in context with useTheme. Tamagui themes operate much
the same as
CSS variables 
do, and so
they can nest and override each other contextually.
Note that the object that useTheme returns is the current theme, proxied upwards to every parent theme, finally proxying back to your tokens. This means it behaves just like CSS variables at well, at runtime.
A short example:
The useTheme hook returns your Theme turned into a ThemeParsed, which means
it turns all values into a Variable:
The useTheme hook further adds a .get() helper function on each Variable for
extra performance. On the web, this will return the .variable property and
avoid re-rendering if values change, as it assumes you are using the CSS
variables it generates to update styles. Meanwhile on native it will return
.val and re-render always. But - if you enable the
fastSchemeChange setting on createTamagui
then get() will return a
DynamicColorIOS  on iOS and avoid
re-rendering there too - unless you do get('web') in which case it will only
optimize for web.
You can mix and match useMedia and useTheme, and the compiler understands
most basic usages, even with nested logic or constants within the file or
imports white-listed in your build setup:
This will compile on the web to:
And the following CSS:
Using outside of styling
You can useTheme() (and useMedia()) at runtime. Like useMedia, useTheme will
only re-render when it has to, and often you can skip re-renders altogether by
either passing the values to a Tamagui styled component, or by using the helper
getVariable:
getVariable
If you access theme.bg.val in your render function, the component will only
re-render when theme.bg changes.
Changing the theme at the hook level
This is a more advanced use for building custom hooks or components, but you can pass in a theme name and a component theme name (as discussed here) to grab the right subset theme:
For example, if you have the following themes:
- dark
- dark_green
- dark_green_MyComponent
And this code:
The above useTheme hook would return the dark_green_MyComponent theme.
Previous
useMedia
Next
FontLanguage