styling-nativewind-v4-expo
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseNativeWind v4 for Expo (React Native)
适用于Expo(React Native)的NativeWind v4
Non‑negotiables (v4)
必须遵守的规则(v4版本)
- Use Tailwind CSS v3 and include in
presets: [require("nativewind/preset")].tailwind.config.js - Keep exactly one Tailwind entry CSS file (commonly ) and keep its path consistent across:
global.css- →
metro.config.jswithNativeWind(..., { input: "./global.css" }) - your app entry → (or
import "./global.css"fromimport "../global.css")app/_layout.tsx
- Keep in Babel
nativewind/babeland setpresetsonjsxImportSource: "nativewind".babel-preset-expo - After any config change, restart Metro without cache: .
npx expo start --clear
- 使用Tailwind CSS v3,并在中添加
tailwind.config.js。presets: [require("nativewind/preset")] - 仅保留一个Tailwind入口CSS文件(通常为),并确保其路径在以下位置保持一致:
global.css- →
metro.config.jswithNativeWind(..., { input: "./global.css" }) - 应用入口文件 → (或从
import "./global.css"中导入app/_layout.tsx)import "../global.css"
- 在Babel的中保留
presets,并在nativewind/babel上设置babel-preset-expo。jsxImportSource: "nativewind" - 任何配置变更后,清除缓存重启Metro:。
npx expo start --clear
Quick start checklist
快速启动检查清单
Copy/paste and tick off:
- Install deps (NativeWind + Tailwind + peers). See .
references/expo-setup.md - Create/verify (content globs +
tailwind.config.js).nativewind/preset - Create/verify with Tailwind directives.
global.css - Create/verify (jsxImportSource +
babel.config.js).nativewind/babel - Create/verify (wrap config with
metro.config.js, setwithNativeWind).input - If targeting web, set →
app.json.expo.web.bundler = "metro" - If TypeScript, add with
nativewind-env.d.ts./// <reference types="nativewind/types" /> - Start with cache cleared and validate on-device + web: .
npx expo start --clear - Validate with an obvious “smoke test” screen: background colour + centred text.
复制粘贴并逐一勾选:
- 安装依赖(NativeWind + Tailwind + 对等依赖)。详见。
references/expo-setup.md - 创建/验证(内容匹配规则 +
tailwind.config.js)。nativewind/preset - 创建/验证包含Tailwind指令的。
global.css - 创建/验证(配置jsxImportSource +
babel.config.js)。nativewind/babel - 创建/验证(用
metro.config.js包装配置,设置withNativeWind路径)。input - 如果面向Web平台,在中设置
app.json。expo.web.bundler = "metro" - 如果使用TypeScript,添加并写入
nativewind-env.d.ts。/// <reference types="nativewind/types" /> - 清除缓存启动应用,并在设备端和Web端验证:。
npx expo start --clear - 通过一个直观的“冒烟测试”页面验证:设置背景色 + 居中文字。
Project type selection
项目类型选择
- Expo Router: entry is usually → import CSS there (relative path is typically
app/_layout.tsx).../global.css - Classic: entry is usually → import CSS there (
App.tsx)../global.css
If unsure, search for .
package.json"main": "expo-router/entry"- Expo Router:入口通常为→ 在此处导入CSS(相对路径通常为
app/_layout.tsx)。../global.css - 传统项目:入口通常为→ 在此处导入CSS(
App.tsx)。./global.css
若不确定,可在中搜索来判断。
package.json"main": "expo-router/entry"Implementation patterns
实现模式
Build reusable components (recommended)
构建可复用组件(推荐)
Accept , merge defaults, and optionally use a class-variance helper.
classNameRead:
references/patterns.md接收属性,合并默认样式,可选择使用类变体辅助工具。
className详见:
references/patterns.mdStyle third‑party components (only when necessary)
为第三方组件设置样式(仅必要时使用)
Use (multiple style props) or (map a class prop to a style prop).
remapPropscssInteropRead:
references/third-party-components.md使用(处理多个样式属性)或(将类属性映射到样式属性)。
remapPropscssInterop详见:
references/third-party-components.mdDark mode + theming
深色模式 + 主题定制
Use / and CSS variables via .
useColorSchemecolorScheme.set()vars()Read:
references/theming-dark-mode.md使用 / 以及通过实现的CSS变量。
useColorSchemecolorScheme.set()vars()详见:
references/theming-dark-mode.mdSafe area utilities
安全区域工具
On Expo Router, do not add your own (Router already does).
Use , , etc.
SafeAreaProviderp-safept-safeIf you are not using Expo Router, wrap the root with .
SafeAreaProvider在Expo Router中,不要自行添加(Router已内置该功能)。
使用、等工具类。
SafeAreaProviderp-safept-safe若未使用Expo Router,请在根组件外层包裹。
SafeAreaProviderTroubleshooting workflow (always in this order)
故障排查流程(请严格按此顺序执行)
- Start Expo without cache: .
npx expo start --clear - Verify Tailwind CLI works by compiling your CSS entry file to an output file.
- Confirm the “three paths” match:
- CSS file exists
metro.config.jspoints to itinput- your app imports it from the entry component
- Confirm
tailwind.config.jsglobs include every directory that containscontentstrings.className - Only then debug platform-specific behaviour (web bundler, Router, safe area, etc).
Read:
references/troubleshooting.md- 清除缓存启动Expo:。
npx expo start --clear - 编译CSS入口文件到输出文件,验证Tailwind CLI是否正常工作。
- 确认“三个路径”一致:
- CSS文件存在
- 中的
metro.config.js路径指向该CSS文件input - 应用入口组件正确导入该CSS文件
- 确认中的
tailwind.config.js匹配规则包含所有包含content字符串的目录。className - 仅在完成上述步骤后,再排查平台特定问题(Web打包工具、Router、安全区域等)。
详见:
references/troubleshooting.mdTHE EXACT PROMPT — NativeWind v4 config audit
精确检查指令 — NativeWind v4配置审计
Use this prompt to perform a deterministic audit of an existing repo:
You are auditing an Expo React Native repo for NativeWind v4 correctness.
1) Identify whether the project uses Expo Router (app/ directory + package.json main = expo-router/entry) or classic App.tsx.
2) Check and report on:
- tailwind.config.js: presets + content globs
- global.css: Tailwind directives exist
- babel.config.js: jsxImportSource nativewind + nativewind/babel in presets; preserve any existing required plugins
- metro.config.js: withNativeWind wrapper; input path matches the CSS file
- app.json: web bundler metro when web is used
- TypeScript: nativewind-env.d.ts present and correctly named
3) For every issue, propose the minimal diff needed to fix it.
4) End by listing the exact commands to restart Metro and validate the fix.使用以下指令对现有仓库进行确定性审计:
You are auditing an Expo React Native repo for NativeWind v4 correctness.
1) Identify whether the project uses Expo Router (app/ directory + package.json main = expo-router/entry) or classic App.tsx.
2) Check and report on:
- tailwind.config.js: presets + content globs
- global.css: Tailwind directives exist
- babel.config.js: jsxImportSource nativewind + nativewind/babel in presets; preserve any existing required plugins
- metro.config.js: withNativeWind wrapper; input path matches the CSS file
- app.json: web bundler metro when web is used
- TypeScript: nativewind-env.d.ts present and correctly named
3) For every issue, propose the minimal diff needed to fix it.
4) End by listing the exact commands to restart Metro and validate the fix.