Expo Router
Installation
1. Install NativeWind
You will need to install nativewind@^4.0.36
and it's peer dependencies tailwindcss
and react-native-reanimated
.
- npm
- yarn
- pnpm
- Expo
npm install nativewind@^4.0.36 react-native-reanimated tailwindcss
yarn add nativewind@^4.0.36 react-native-reanimated tailwindcss
pnpm install nativewind@^4.0.36 react-native-reanimated tailwindcss
npx expo install nativewind@^4.0.36 react-native-reanimated tailwindcss
Run pod-install
to install Reanimated pod:
npx pod-install
2. Setup Tailwind CSS
Run npx tailwindcss init
to create a tailwind.config.js
file
Add the paths to all of your component files in your tailwind.config.js file.
tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
// NOTE: Update this to include the paths to all of your component files.
content: ["./app/**/*.{js,jsx,ts,tsx}"],
presets: [require("nativewind/preset")],
theme: {
extend: {},
},
plugins: [],
}
Create a CSS file and add the Tailwind directives
global.css
@tailwind base;
@tailwind components;
@tailwind utilities;
info
From here onwards, replace ./global.css
with the relative path to the CSS file you just created
3. Add the Babel preset
- SDK 49
- SDK 50+
babel.config.js
module.exports = function (api) {
api.cache(true);
return {
presets: [
["babel-preset-expo", { jsxImportSource: "nativewind" }],
"nativewind/babel",
],
plugins: [
// Required for expo-router
"expo-router/babel",
"react-native-reanimated/plugin",
]
};
};
babel.config.js
module.exports = function (api) {
api.cache(true);
return {
presets: [
["babel-preset-expo", { jsxImportSource: "nativewind" }],
"nativewind/babel",
],
};
};
4. Modify your metro.config.js
If your project does not have a metro.config.js
run npx expo customize metro.config.js
- SDK 49
- SDK 50+
metro.config.js
const { getDefaultConfig } = require("expo/metro-config");
const { withNativeWind } = require('nativewind/metro');
const config = getDefaultConfig(__dirname, { isCSSEnabled: true })
module.exports = withNativeWind(config, { input: './global.css' })
metro.config.js
const { getDefaultConfig } = require("expo/metro-config");
const { withNativeWind } = require('nativewind/metro');
const config = getDefaultConfig(__dirname)
module.exports = withNativeWind(config, { input: './global.css' })
5. Import your CSS file
app/_layout.js
import Slot from "expo-router/Slot";
// Import your global CSS file
import "../global.css"
export default Slot
6. TypeScript (optional)
Please follow the TypeScript guide.