SSR
Unistyles 3.0 is fully compatible with Next.js Server Side Rendering (SSR). Components styled with Unistyles are rendered on the server and their CSS is injected into the HTML.
Next.js setup
Section titled “Next.js setup”Unistyles relies on its Babel plugin, so Next.js has to compile your app with Babel and webpack instead of SWC and Turbopack.
Next.js picks up a babel.config.js automatically. Set root to the folder with your components:
module.exports = api => { api.cache(true)
return { presets: ['next/babel'], plugins: [['react-native-unistyles/plugin', { root: 'app' }]] }}Then resolve react-native to react-native-web and define __DEV__, which React Native libraries expect:
const webpack = require('webpack')
module.exports = { transpilePackages: ['react-native-web', 'react-native-unistyles'], webpack: (config, { dev }) => { config.resolve.alias = { ...config.resolve.alias, 'react-native$': 'react-native-web' } config.resolve.extensions = ['.web.js', '.web.jsx', '.web.ts', '.web.tsx', ...config.resolve.extensions] config.plugins.push(new webpack.DefinePlugin({ __DEV__: JSON.stringify(dev) }))
return config }}Next.js 16 uses Turbopack by default, so run it with webpack: next dev --webpack and next build --webpack.
To use server-side rendered styles, create the following client-side component:
'use client'
import { PropsWithChildren, useRef } from 'react'import { useServerUnistyles } from 'react-native-unistyles/server'import { useServerInsertedHTML } from 'next/navigation'import './unistyles'
export const Style = ({ children }: PropsWithChildren) => { const isServerInserted = useRef(false) const unistyles = useServerUnistyles()
useServerInsertedHTML(() => { if (isServerInserted.current) { return null }
isServerInserted.current = true
return unistyles })
return <>{children}</>}With the component in place, make sure it wraps your body’s children:
import '../unistyles' import { Style } from '../Style'
export default function RootLayout({children,}: Readonly<{children: React.ReactNode;}>) {return ( <html lang="en"> <body> <Style> {children} </Style> </body> </html>);}With this setup, we will ensure that Unistyles is initialized correctly and injects CSS on the server-side.
Keep every component that uses Unistyles styles in a Client Component:
'use client'
import { Text, View } from 'react-native'import { StyleSheet } from 'react-native-unistyles'
export default function Page() { return ( <View style={styles.container}> <Text>Hello from Unistyles</Text> </View> )}
const styles = StyleSheet.create(theme => ({ container: { backgroundColor: theme.colors.background }}))Config (Optional)
Section titled “Config (Optional)”useServerUnistyles accepts an optional config object:
includeRNWStyles– a boolean that enables or disables injecting React Native Web default CSS styles. Defaults totrue.
To use server-side rendered styles, add the following code to your codebase:
import { getServerUnistyles, resetServerUnistyles } from 'react-native-unistyles/server'
export default class Document extends NextDocument { static async getInitialProps({ renderPage }: DocumentContext) { const page = await renderPage() const styles = getServerUnistyles()
resetServerUnistyles()
return { ...page, styles } }And add the following use effect to your _app.tsx
import { hydrateServerUnistyles } from 'react-native-unistyles/server'
{/* JSX of your component */} useEffect(() => { hydrateServerUnistyles() }, [])Config (Optional)
Section titled “Config (Optional)”getServerUnistyles accepts an optional config object:
includeRNWStyles– a boolean that enables or disables injecting React Native Web default CSS styles. Defaults totrue.
Troubleshooting
Section titled “Troubleshooting”Hydration error
Section titled “Hydration error”If you’re not using adaptive themes, you might encounter hydration error on your root html element. This is because unistyles is adding a className to it based on the current theme.
To fix this simply add suppressHydrationWarning to your root html element.
<html lang="en"><html lang="en" suppressHydrationWarning>Or you can directly add the className to your root html element.
<html lang="en"><html lang="en" className="dark">Errors in Server Components
Section titled “Errors in Server Components”Errors like useRef is not a function, Right-hand side of 'instanceof' is not an object or
You're importing a module that depends on useState into a React Server Component module mean that a component styled with Unistyles is rendered inside a React Server Component.
React Server Components are not officially supported. Add 'use client' at the top of the file that renders the styled components.