Expo Router
Expo Router is a popular routing library from Expo that is built on top of React Navigation. When using Unistyles with Expo Router, it’s necessary to configure it properly.
Modify main entry
Section titled “Modify main entry”Expo Router resolves routes differently than expected. Also, Unistyles 3.0 is parsing your StyleSheets as soon as you import file containing it.
This combination may cause some issues. To prevent that you need to modify your main entry file:
{ "main": "expo-router/entry" "main": "index.ts"}Then, create index.ts (or index.js) file with following content:
import 'expo-router/entry'import './unistyles' // <-- file that initializes UnistylesWith this setup, we will ensure that Unistyles is initialized before any other component.
Expo Router Web - Static rendering
Section titled “Expo Router Web - Static rendering”You can check if you are using static rendering in app.json:
{ "expo": { "web": { "bundler": "metro", "output": "static" } }}For Expo static rendering, every page will be resolved with the root HTML file. Unfortunately, this file is hidden, and you need to create it manually.
Please follow the Expo guide and add a +html.tsx file.
In this file, initialize Unistyles by importing the config file:
import React from 'react'import { ScrollViewStyleReset } from 'expo-router/html'import { type PropsWithChildren } from 'react' import '../unistyles' // <-- file that initializes Unistyles
export default function Root({ children }: PropsWithChildren) { ...}This ensures that Unistyles is initialized whenever Expo Router renders the next static page.
Expo Router Web - Server rendering
Section titled “Expo Router Web - Server rendering”With server rendering ("output": "server", Expo SDK 58 and later), Expo renders every page on request with the same +html.tsx file.
The server doesn’t run your main entry, so import the config file in +html.tsx, and render the page’s styles with useServerUnistyles:
import '../unistyles' // <-- file that initializes Unistylesimport { useServerDocumentContext } from 'expo-router/html'import { type PropsWithChildren } from 'react'import { useServerUnistyles } from 'react-native-unistyles/server'
// Rendered after the page, so it collects the styles the page renderedconst UnistylesStyles = () => <>{useServerUnistyles({ includeRNWStyles: false })}</>
export default function Root({ children }: PropsWithChildren) { const { htmlAttributes, bodyAttributes, headNodes, bodyNodes } = useServerDocumentContext()
return ( <html {...htmlAttributes}> <head>{headNodes}</head> <body {...bodyAttributes}> {children} <UnistylesStyles /> {bodyNodes} </body> </html> )}If you don’t use adaptive themes, add the class of your initial theme to <html>, next to the class Expo may set, so that the page uses its CSS variables before the JavaScript loads:
<html {...htmlAttributes}><html {...htmlAttributes} className={[htmlAttributes?.className, 'light'].filter(Boolean).join(' ')}>useServerDocumentContext already includes react-native-web styles in headNodes, so includeRNWStyles is disabled.
In the browser, Unistyles takes over the server styles on its own, so you don’t need to call hydrateServerUnistyles.
The server doesn’t know the browser’s color scheme, screen size or breakpoint. Values read during render, e.g. with useUnistyles, can differ between the server’s HTML and the browser’s first render.
Keep styles that depend on them in StyleSheet (breakpoints, media queries, adaptive themes), which the server renders as CSS.