Next.js Guide

How to set up Tamagui with Next.js

Running npm create tamagui let's you choose the next-expo-solito starter which is a very nicely configured Next.js app where you can take or leave whatever you want.

Also check out the source for this site  to see a good example of a full featured Next.js website as well, especially the next.config.js and tamagui.config.ts.


To configure Tamagui for Next.js, you'll want to add the @tamagui/next-plugin and set it up in your next config. See the compiler install docs for more options:

module.exports = function (name, { defaultConfig }) {
let config = {
// ...your configuration
const tamaguiPlugin = withTamagui({
config: './tamagui.config.ts',
components: ['tamagui'],
// build-time generate CSS styles for better performance
// we recommend only using this for production so you get reloading during dev mode
outputCSS: process.env.NODE_ENV === 'production' ? './public/tamagui.css' : null,
return {


Add the reset to get more consistent styles accross browsers:

import '@tamagui/core/reset.css'

And then add this if you are using the outputCSS option to include the CSS file generated at build-time:

if (process.env.NODE_ENV === 'production') {


You'll want to set up a _document.tsx and gather both the react-native-web style object using AppRegistry, as well as Tamagui styles using Tamagui.getCSS() into the head element.

Make sure the Tamagui style tag always comes after the getStyleElement style tag to ensure Tamagui styles override built-in react-native-web ones.

Note that we only are importing react-native here because if you use tamagui views like Input or Image they rely on React Native Image/Input (and therefore on the web, react-native-web). If you are just using core, or aren't using Image or Input in tamagui, you can forego the entire AppRegistry import and setup here. We're working on making even tamagui completely de-coupled from RN/RNW by building their own image and input components eventually.

import NextDocument, { Head, Html, Main, NextScript } from 'next/document'
import { Children } from 'react'
import { AppRegistry } from 'react-native'
import Tamagui from '../tamagui.config'
export default class Document extends NextDocument {
static async getInitialProps({ renderPage }) {
AppRegistry.registerComponent('Main', () => Main)
const page = await renderPage()
// @ts-ignore
const { getStyleElement } = AppRegistry.getApplication('Main')
const styles = [
<style dangerouslySetInnerHTML={{ __html: Tamagui.getCSS({ // if you are using "outputCSS" option, you should use this "exclude" // if not, then you can leave the option out exclude: process.env.NODE_ENV === 'production' ? 'design-system' : null, }), }} />,
return { ...page, styles: Children.toArray(styles) }
render() {
return (
<Main />
<NextScript />


We've created a package that works with Tamagui to properly support SSR light/dark themes that also respect user system preference, called @tamagui/next-theme. It assumes your light/dark themes are named as such, but you can override it. This is pre-configured in the create-tamagui starter.

We recommend memo-ing children so they don't re-render. Here's how you'd set up your _app.tsx:

import { NextThemeProvider, useRootTheme } from '@tamagui/next-theme'
import { AppProps } from 'next/app'
import Head from 'next/head'
import React, { useMemo, useState } from 'react'
import { TamaguiProvider } from 'tamagui'
import config from '../tamagui.config'
export default function App({ Component, pageProps }: AppProps) {
const [theme, setTheme] = useRootTheme()
// memo to avoid re-render on dark/light change
const contents = useMemo(() => {
return <Component {...pageProps} />
}, [pageProps])
// because we do our custom getCSS() above, we disableInjectCSS here
return (
<Head>{/* ... */}</Head>
<NextThemeProvider onChangeTheme={setTheme}>
<TamaguiProvider config={config} disableInjectCSS disableRootThemeClass defaultTheme={theme} >

If you need to access the current theme, say for a toggle button, you will then use the useTheme hook. We'll release an update in the future that makes this automatically work better with Tamagui's built-in useTheme.

import { useTheme } from '@tamagui/next-theme'
export default () => {
const { theme, toggleTheme } = useTheme()
return <Button>Change theme (currently: {theme})</Button>

Mount animations

Animations that run through JS drivers and have enterStyle set will want to add the following script that allows for hiding those animations before the JS runs. This is the "right way" to handle things, as it allows for disabling JS entirely and not accidentally hiding all unmounted things. Meanwhile, it still properly avoids a flash of mounted style for SSR pages.

Add this to your _app.tsx render output:

<script key="tamagui-animations-mount" dangerouslySetInnerHTML={{ // avoid flash of animated things on enter __html: `document.documentElement.classList.add('t_unmounted')`, }} />

Server components

Tamagui includes Server Components support for the Next.js app directory. It should work fully server side eventually, but we have to work with the Next.js team to figure out the final steps here. Until then, use client support is here already.

Create a new component:

'use client'
import { useServerInsertedHTML } from 'next/navigation'
import { TamaguiProvider } from 'tamagui'
export function NextTamaguiProvider({ children }) {
useServerInsertedHTML(() => {
// this first time this runs you'll get the full CSS including all themes
// after that, it will only return CSS generated since the last call
return <>{Tamagui.getNewCSS()}</>
// see Tamagui provider setup in the example above
return <TamaguiProvider>{children}</TamaguiProvider>

The new getNewCSS helper in Tamagui will keep track of the last call and only return new styles generated since the last usage.

And then use it in your app/layout.tsx like this:

import { NextTamaguiProvider } from './NextTamaguiProvider'
export default function Layout({ children }) {
return <NextTamaguiProvider>{children}</NextTamaguiProvider>