别让 AI 把 Expo 写成裸 React Native:三个最容易退回通用方案的瞬间

原创2026-10-29 19:48:211,937 阅读
文章标签:AI 技能AI 插件移动开发跨平台

别让 AI 把 Expo 写成裸 React Native:三个最容易退回通用方案的瞬间

AI 编程助手在 Expo 项目里翻车,很少是因为“不会写 React Native”。恰恰相反——它对 React Native 的通用写法太熟了。PanResponder、Animated、AsyncStorage、@gorhom/bottom-sheet、expo-linear-gradient,这些名字在训练语料里出现的频率远高于 Expo 官方当前推荐的写法,于是模型一旦遇到不确定的地方,就会本能地滑回那个“更熟”的版本。

Expo 官方开源的 skills 仓库 正是针对这个失效模式设计的。它不是一个“Expo 知识大全”,而是一套边界约束:每个 SKILL.md 用 frontmatter 的 description 声明触发条件,用正文里的 Hard Rules / Never Ship 表声明不可逾越的线,再用 expo-overview 做统一路由。这套结构里藏着大量“反通用方案”的具体条文,恰好对应了 AI 最容易跑偏的三个瞬间。

下面这三个瞬间,都能在仓库源码里找到明确证据。

退化从哪里开始:模型先验强于项目事实

expo-overview 是整个技能集的入口,它的定位写在 description 里:任何提到 Expo、EAS、Expo Go、expo-* 包,或者 package.json 里存在 expo 依赖的任务,都必须先经过它。

它只做三件很“不像 AI”的事:

  1. 确认这确实是 Expo/EAS 工作——否则技能不适用,纯 React Native 项目不该被套上 Expo 规则;
  2. 读用户的真实目标,再用 Skill Map 翻译成具体技能;
  3. 相信叶子技能(step 5 的原文是“Trust the leaf skill — it has its own detection logic and steps. Don't improvise.”)。

第三步是关键。AI 出问题的典型路径不是“选错技能”,而是“选了技能之后仍然自己发挥”。expo-overview 在开头就写死了一句警告:

Do not guess the skill from project files alone. Many Expo goals look similar from the filesystem but need different skills.

这条规则同样适用于“不要只看代码猜 API”。文件系统里看不出 SDK 版本,看不出项目是 CNG 还是已提交 ios/、android/ 的 prebuild 状态,也看不出这台机器上装的是 Reanimated 3 还是 4——而这些恰恰决定了正确写法。

于是同一份 SKILL.md 里紧接着规定了共享前置动作:读 package.json 里的 expo 版本;读对应版本的文档而不是 latest(原文举例:SDK 56 上应看 https://docs.expo.dev/versions/v56.0.0/sdk/ui/,因为 latest 会描述项目还没有的 API);判断 managed / prebuild;以及本文第一个瞬间的核心——装包必须走 npx expo install。

瞬间一:让它装依赖时绕过了官方 CLI

这是三个瞬间里最隐蔽的一个。AI 写出 npm install react-native-reanimated 时,代码本身看起来毫无问题,package.json 也更新了,npm run start 甚至能跑起来。问题要到原生构建、运行时崩溃或者 expo-doctor 报警时才暴露。

仓库里对这条规则的表述异常统一,出现在至少五个不同位置:

plugins/expo/skills/expo-overview/SKILL.md 的共享规则:

- **Install packages with `npx expo install <pkg>`**, not raw `npm`/`yarn`/`pnpm add`,
  so versions stay compatible with the project's SDK.

plugins/expo/skills/expo-animation/SKILL.md 把原因说透了:

Install with npx expo install <package> — it resolves the version that matches the project's SDK, which plain npm install won't.

在 plugins/expo/skills/expo-animation/RECIPES.md 里,这句话被压缩成一句括号注释,专门用来拦截复制粘贴行为:

(`react-native-keyboard-controller` only for the keyboard recipe.) `expo install`, not `npm install`
— it resolves the versions that match the SDK.

plugins/expo/skills/expo-brownfield/references/troubleshooting.md 则给出了排障场景的版本:

Install with npx expo install <package> rather than plain yarn add — expo install picks the version compatible with the current SDK.

还有一个最容易被忽略的变体,写在 plugins/expo/skills/expo-examples/SKILL.md 里。当 AI 参考 expo/examples 里的现成示例时,看到的是别人项目的 package.json:

Version-align — don't copy pinned versions. Examples track the latest SDK, so their package.json pins won't match an older project. Add only the missing deps with npx expo install <pkg> (it resolves SDK-correct versions) instead of copying exact versions.

这里其实包含两个退化点:不走 expo install,以及照抄示例里的版本号。后者更危险,因为它看起来像是在“严格遵循官方示例”。

约束该怎么落地?把 expo-overview 的共享规则前移到项目根目录的 AGENTS.md / CLAUDE.md,写成无条件的硬规则,并明确它不因任务“够具体”而失效。expo-overview 里专门堵了这个漏洞:

A fully-specified task (SDK version pinned, file layout given, libraries named) is not a reason to skip: the shared rules above still apply — check them, then route to the matching leaf skill.

配套的验收动作也要一并写进去:装完依赖跑 npx expo-doctor;升级后跑 npx expo install --fix 对齐版本。plugins/expo/skills/eas-observe/references/setup.md 里就是这三步的连用形式:

npx expo install --fix
npx expo install expo-observe

瞬间二:涉及资源文件或动画手势时误用通用方案

第二个瞬间更容易被“能跑”掩盖。动画在模拟器里看着还行,图标在 iOS 上显示正常,渐变颜色也对——只有在真机、Android、旧架构或者暗色模式下才出问题。

动画与手势:Never Ship 表

plugins/expo/skills/expo-animation/SKILL.md 是仓库里态度最强硬的一份文件,它的 Hard Rules 第 2 条直接点名:

Reanimated, not core Animated. Core Animated can't be driven by a gesture without crossing the bridge, and useNativeDriver refuses anything but transform and opacity anyway.

它甚至规定了两个失败模式的严重性排序:第一严重的是“给不该动的东西加动画”,第二才是“在错误的线程上动”。文中的判断回路非常工程化——按触发频率分档,每秒发生上百次的交互(切 Tab、键盘开合、滚动)一律不加动画;切 Tab 明确要求 animation: 'none',理由是“Tab 是平级关系,滑动暗示了并不存在的层级”。

真正拦住 AI 的是文末那张 Never Ship 表。它不是建议,而是黑名单:

Never Instead
PanResponder Gesture.Pan() from gesture-handler
setState in a gesture or scroll handler shared value + useAnimatedStyle
runOnJS (deprecated in Reanimated 4) scheduleOnRN from react-native-worklets
Reading or writing a shared value during render .get() / .set() in worklets, handlers, effects
Core Animated for anything a finger touches Reanimated
Animating height / width / margin / flex / top transform + opacity
A screen transition rebuilt in JS native stack animation
Judging feel in Expo Go or the simulator release build, slowest supported device

这张表的价值在于:每一条都对应一个 AI 的高频默认动作。PanResponder 和 setState 驱动动画是 React Native 教程里的经典写法;runOnJS 在 Reanimated 3 时代是标准答案,在 4 里已被废弃;.value 是几乎所有存量代码的写法,而 Reanimated 文档把 .get() / .set() 称为 compiler-safe 的形式——因为 React Compiler 看不穿直接访问 .value。

还有一类“配置型”退化,专挑 AI 自信满满的时候发生:

// 错:Reanimated 4 会拒绝原始字符串
style={{ transitionTimingFunction: 'cubic-bezier(0.23, 1, 0.32, 1)' }}

// 对:CSS transition 用 cubicBezier(...)
import { cubicBezier } from 'react-native-reanimated';
const CSS_EASE_OUT = cubicBezier(0.23, 1, 0.32, 1);

技能里明确写了“Reanimated 4.1.1 和 4.5.1 会拒绝原始 'cubic-bezier(...)' 字符串”,并且区分了 cubicBezier(...)(用于 CSS transition)与 Easing.bezier(...)(用于 withTiming)。这种颗粒度的信息,模型靠先验是补不出来的。

至于“动画就是不动”,技能把排查顺序也写死了,第一条就是安装方式:用 npx expo install 装 react-native-reanimated 与 react-native-worklets,在 Expo 项目里 babel-preset-expo 会自动配置 worklets 插件,不需要手写 babel.config.js;只有不带该 preset 的裸 RN 项目才需要手动加插件,且必须放在列表最后。写错的后果不是静默降级,而是运行时抛出 Failed to create a worklet。

组件:props 写错会静默失效

plugins/expo/skills/expo-ui/SKILL.md 记录了一个非常典型的“通用方案残留”。@expo/ui 的 BottomSheet 和 @gorhom/bottom-sheet 长得像,但控制属性完全不同:

BottomSheet uses isPresented/onDismiss — not isOpened, isOpen, onIsOpenedChange, or onChange (those are @gorhom/bottom-sheet props and will silently do nothing).

“静默无效”是 AI 生成代码里最难排查的一类缺陷:没有报错,没有警告,只是弹不出来。同一份文件里还纠正了另一个语义误用——@expo/ui 的 List 渲染的是原生分组行(类似 iOS 设置页),不是虚拟化列表,长列表要用 FlatList / FlashList。

expo-overview 则把组件选择顺序上升为路由级规则:

whenever you need a UI component (list rows, bottom sheets, pickers, sliders, menus, buttons, segmented controls, toggles), consult expo-ui first to check whether @expo/ui has a native equivalent before reaching for a React Native built-in or a community library.

资源与样式:被废弃的包名是重灾区

plugins/expo/skills/expo-upgrade/SKILL.md 里有一张“废弃包 → 替代品”表,几乎就是一份 AI 常用库的黑名单:

Old Package Replacement
expo-av expo-audio and expo-video
expo-permissions Individual package permission APIs
@expo/vector-icons expo-symbols (for SF Symbols)
AsyncStorage expo-sqlite/localStorage/install
expo-app-loading expo-splash-screen
expo-linear-gradient experimental_backgroundImage + CSS gradients in View

每一条背后都有具体的“通用方案陷阱”:

  • 渐变:plugins/expo/skills/expo-native-ui/references/gradients.md 开头就限定“CSS 渐变仅支持新架构(Fabric),旧架构与 Expo Go 不可用”,并在文末写明 Do NOT use expo-linear-gradient。改用 experimental_backgroundImage 后,渐变变成了一个样式字符串,多段渐变靠逗号叠加。
  • 图标:plugins/expo/skills/expo-native-ui/references/icons.md 的第一句是“SF Symbols 是 Apple 资产,在 Android 上永远不渲染”。所以 expo-symbols 的 SymbolView 必须配 Material 对应项;iOS 用 SF Symbols,Android 用 Material,两端都不能用 FontAwesome / Ionicons,也不能用 emoji 当图标。design-system 技能甚至把这个现象命名为 “Emoji Iconography”,列进 AI 生成应用的 20 个典型痕迹之一。
  • Android 矢量资源:plugins/expo/skills/expo-ui/references/jetpack-compose.md 给了一条 AI 基本不会想到的路径——<Icon source={require('./icon.xml')} />,把 Material Symbols 下载成 XML vector drawable 放进 assets/icons/,Metro 会自动打包 .xml,无需改 metro 配置。通用方案会在这里塞一个网络图片或者图标字体。
  • 图片:plugins/expo/skills/expo-native-ui/SKILL.md 要求用 expo-image 的 Image 代替内建 img,并在 false-friends 映射里补充了坑点——远程图片必须显式给 width / height 或者一个 flex 父容器。
  • 存储:plugins/expo/skills/expo-native-ui/references/storage.md 第 5 行只有一句加粗结论:“Use the localStorage polyfill for key-value storage. Never use AsyncStorage”。落地方式是 import "expo-sqlite/localStorage/install",让存量 Web 代码原样跑起来。

plugins/expo/skills/expo-web-to-native/references/false-friends.md 把这些散点系统化成了一张“看起来能移植、编译得过、原生上炸掉”的对照表,值得单独作为 AI 的上下文附件喂进去。几个高频项:

Web 写法 原生等价物 坑
<div> / <span> <View> 没有语义化标签变体
<p> / <h1> / JSX 裸字符串 <Text> 所有文本都必须在 <Text> 内,裸字符串直接抛错
className / CSS 文件 StyleSheet.create 或 NativeWind RN 没有 className
display: flex 可选、默认 row flex 常开、默认 column 最大的布局意外
localStorage expo-sqlite 的 globalThis.localStorage 有 polyfill,但没有 sessionStorage 等价物
fetch('/api/x') 相对路径 绝对 URL 原生没有 origin,相对路径必失败
流式响应(SSE / AI SDK useChat) expo/fetch 的 textStreaming RN 内建 fetch 读不了流式 body

最后一行尤其值得注意:expo-data-fetching 的 Preferences 只有一行——“Avoid axios, prefer expo/fetch”。模型默认会装 axios,而真正需要的是能被流式读取的 expo/fetch。

瞬间三:报错后自行重构成老写法,怎么用约束拦住

前两个瞬间是“生成时跑偏”,第三个是“修错时跑偏”,杀伤力更大,因为它披着“我在解决问题”的外衣。

典型剧本:Reanimated 相关的报错出现,AI 判断“动画库版本不兼容”,于是把 useSharedValue + useAnimatedStyle 改成 Animated.Value + useNativeDriver;或者 expo-audio 的 API 不熟,就把 expo-av 装回来;或者 header 配置报错,就 headerShown: false 加一个自绘标题栏。每一步都让当前报错消失,同时把项目推回裸 React Native。

仓库对这类行为的约束是分层的,值得照搬到自己的项目里。

第一层:禁止即兴。

expo-overview 的 step 5 已经写了“Don't improvise”。expo-animation 则在开篇设定了角色姿态:

You are a senior mobile engineer building the animation yourself. Make the call, state the reasoning in one line, write the code. Never present motion options as a menu.

“Never present motion options as a menu”这条很关键。当 AI 把“用 Reanimated 还是用核心 Animated”作为一个选项抛回给用户时,实际上已经把决策权让渡给了不确定的一方。

第二层:把“重构”引导到受支持的任务上。

expo-upgrade 处理的是“必须动版本”的场景,而 expo-overview 明确把这类动作从日常任务里剥离:

Moving to a newer SDK is its own task — load expo-upgrade instead of bumping versions by hand.

expo-upgrade 里也塞了硬性警告,防止 AI 用“降版本”当解法。例如 SDK 55/56 与早期 SDK 57 存在 Hermes V1 内存回归,正确动作是直接升到 SDK 57 且 expo@57.0.9 以上,而不是回退:

If upgrading from SDK 55 or earlier, skip SDK 56 and upgrade directly to SDK 57. Don't use expo@57.0.8 or below.

同一份文件的 Housekeeping 段落甚至覆盖了那种“看着不像代码所以随便删”的行为:babel.config.js 若只包含 babel-preset-expo 就删除,metro.config.js 若只有 Expo 默认值就删除;但在删依赖之后必须立刻跑 npx expo-doctor,把它报告为缺失 peer 的包加回来。

第三层:让反馈闭环回到技能本身。

这是这个仓库最容易被忽略的设计。每一个 SKILL.md 结尾都有一段完全一致的 Submitting Feedback,由 CI 强校验:

npx --yes @expo/agent-cli@latest feedback --category skills --subject "<skill-name>" "<actionable feedback>"

并且区分了两种上报语义:技能内容错误用 --category skills;如果 agent 反复失败、或者用户不得不接管,用 --category evals,并把相关的 Expo 包或命令作为 subject。

最后这一句其实是对“第三个瞬间”的正面回应——它承认技能可能不够,但要求把失败变成可追踪的样本,而不是让 AI 悄悄降级代码。

第四层:CI 守住文档结构,防止规则本身腐烂。

约束要长期有效,前提是它不会随着时间被稀释。仓库里两个脚本承担这件事。

scripts/check-overview-routing.ts 检查“每个技能都能被路由到”,实现只有几行:

const skillNames = listSkillNames();
const router = readFileSync(ROUTER_PATH, "utf8");

// A skill is "routed" when it appears as an inline-code token: `skill-name`.
const missing = skillNames.filter((name) => !router.includes(`\`${name}\``));

新增技能若没写进 expo-overview 的 Skill Map,CI 直接失败。这保证了“入口永远知道所有出口存在”。

scripts/check-skill-limits.ts 守的是另外两条线:SKILL.md 正文不超过 500 行、description 不超过 1024 字符,并且校验每个技能都带规范的反馈块(--fix-feedback 可自动补齐)。这解释了为什么这个仓库里的技能都把细节塞进 references/ 而不是堆在正文——主文件只留判断规则,长表格按需加载。

约束怎么写才有效:三个可复用的动作

把上面三个瞬间合起来看,能提炼出几条可以直接抄进自己 AGENTS.md 的原则。

一、规则要写成黑名单,不要写成偏好。 “推荐使用 Reanimated”这种措辞对模型几乎无效;“PanResponder 不允许出现”才有效。expo-animation 的 Never Ship 表、expo-upgrade 的废弃包表、expo-native-ui 的 “Never use modules removed from React Native such as Picker, WebView, SafeAreaView, or AsyncStorage”,都是可执行的形状。

二、为每个黑名单条目给出替代品和失败症状。 只说“不要用 @gorhom/bottom-sheet”,AI 会换个库;“用 @expo/ui 的 BottomSheet,属性是 isPresented/onDismiss,写成 isOpen 会静默无效”,才是可执行的知识。false-friends 那张表的每一行都是这个结构:Web 写法 → 原生等价物 → 坑。

三、把“验证”写成任务的一部分,而不是建议。 expo-animation 要求最终输出包含“该在真机上手测什么”——快速滑动、中途打断、反向操作、在三年前的低端 Android 上跑一遍;expo-native-ui 则要求在一个屏幕被判定完成前,走一遍主任务、一次失败与恢复、键盘可达性、返回/关闭行为、长标题、缺图、空搜索结果、大字号。这些动作无法靠编译通过来替代,也正好是“退回通用方案”最容易暴露的环节。

Expo Skills 的真正价值不在于让 AI 多知道几个 API。它做的是把“什么情况下不许动手”“什么情况下必须换任务”“什么情况下要承认失败并上报”写成结构化的、被 CI 守护的文本。对已经在用 AI 写 Expo 的团队来说,这套约束比任何一份提示词模板都更接近工程实践——因为它约束的不是模型的表达,而是模型的退路。

登录后查看全文
skills