Metro integration
react-native-unistyles/metro replaces the Babel plugin with a single line in metro.config.js. There is nothing to configure: no root, autoProcessImports, autoProcessPaths or autoRemapImports.
const { getDefaultConfig } = require('expo/metro-config')// or for bare React Native// const { getDefaultConfig } = require('@react-native/metro-config')const { withUnistylesConfig } = require('react-native-unistyles/metro')
const config = getDefaultConfig(__dirname)
module.exports = withUnistylesConfig(config)Then remove react-native-unistyles/plugin from your babel.config.js. Unistyles throws an error if both are set up.
What it does
Section titled “What it does”The Metro integration doesn’t run any Babel transform, it covers both jobs of the Babel plugin at runtime:
- Every
react-nativeimport resolves to Unistyles components. A Metro resolver swapsView,Text,Pressableand the other supported components for Unistyles components. It also coversnode_modules, so a 3rd party component that passes yourstyleto a React Native view updates with your theme, withoutwithUnistyles. The internals of React Native, React Native Web and Unistyles stay untouched. - StyleSheets record their dependencies while they run. Instead of reading the dependencies detected by the Babel plugin, Unistyles tracks what your StyleSheet reads from the mini runtime:
rt.screensubscribes it to screen changes,rt.insets.imeto the keyboard and so on. A StyleSheet function always depends on the theme. Dependencies read behind a condition, in a helper function or in a dynamic function are picked up as well, the first time the code reads them.
Unistyles can’t tell which style of a StyleSheet read a value, so when anything a StyleSheet function reads changes, Unistyles recomputes every style of that StyleSheet. It only updates the views whose styles came out different, the others stay untouched. On iOS and Android, the first change after a view mounts updates it either way, as Unistyles compares with what it applied itself. On iOS and Android, a component wrapped with withUnistyles re-renders for theme changes, and for other changes only when its styles came out different.
Reads inside a dynamic function or a getter belong to the views that use that style. For a value that changes on every frame, like rt.insets.ime, read it in a dynamic function, so only the views that call it are recomputed while the keyboard animates. Read it inside the function: destructuring it in the StyleSheet’s arguments, eg. (theme, { insets: { ime } }), reads it for the whole StyleSheet.
const styles = StyleSheet.create((theme, rt) => ({ container: { flex: 1, backgroundColor: theme.colors.background }, // only the views that call styles.keyboard() depend on the keyboard keyboard: () => ({ paddingBottom: rt.insets.ime })}))Variants
Section titled “Variants”Without the Babel plugin, styles.useVariants() doesn’t change styles. Use the styles it returns:
const Button = ({ size }) => { const variantStyles = styles.useVariants({ size })
return <View style={variantStyles.button} />}This form also works with the Babel plugin. The codemod converts your components, reads of styles below the call (in the same block, also in callbacks) read the returned styles:
npx jscodeshift --parser=tsx --extensions=tsx,ts,jsx,js -t node_modules/react-native-unistyles/codemods/use-variants.js srcThe codemod adds semicolons to the lines it changes, run your formatter afterwards.
Other Metro plugins
Section titled “Other Metro plugins”withUnistylesConfig wraps the resolveRequest of the config it receives. Apply it last, or make sure that a plugin applied after it keeps it.
To render a raw React Native component, import it from react-native/index.js, which Unistyles doesn’t swap.
Limitations
Section titled “Limitations”- Metro only. Re.Pack, Rspack and Rsbuild have
UnistylesPlugin, Vite, Next.js and other web bundlers keep using the Babel plugin - A StyleSheet recomputes more styles than with the Babel plugin, see What it does
styles.useVariants()has no effect when the returned styles aren’t used,stylesignores its variants (also the default ones), see Variants- Jest doesn’t use Metro, your testing setup stays the same
- Your app has to install
react-native-unistylesonce, Metro warns about a second copy (eg. nested in a library’snode_modules), its styles won’t update