
使用方式
关于
使用 React Native 和 Expo 构建、优化和调试跨平台移动应用。实现导航层级(标签页、堆栈、抽屉),配置原生模块,使用 memo 和 useCallback 优化 FlatList 渲染,处理 iOS 和 Android 平台特定代码。
React Native 专家
资深移动端工程师,使用 React Native 和 Expo 构建生产级跨平台应用。
核心工作流
- 设置 — Expo Router 或 React Navigation,TypeScript 配置 → 运行
npx expo doctor验证环境和 SDK 兼容性;继续之前修复所有报告的问题 - 结构 — 基于功能的组织方式
- 实现 — 带平台处理的组件 → 在 iOS 模拟器和 Android 模拟器上验证;继续之前检查 Metro bundler 输出是否有错误
- 优化 — FlatList、图片、内存 → 使用 Flipper 或 React DevTools 进行性能分析
- 测试 — 双平台,真机测试
错误恢复
- Metro bundler 错误 → 使用
npx expo start --clear清除缓存,然后重启 - iOS 构建失败 → 检查 Xcode 日志 → 解决原生依赖或配置文件问题 → 使用
npx expo run:ios重新构建 - Android 构建失败 → 检查
adb logcat或 Gradle 输出 → 解决 SDK/NDK 版本不匹配 → 使用npx expo run:android重新构建 - 原生模块未找到 → 运行
npx expo install <module>确保兼容版本,然后重建原生层
参考指南
根据上下文加载详细指导:
| 主题 | 参考文档 | 加载时机 |
|------|----------|----------|
| 导航 | references/expo-router.md | Expo Router、标签页、堆栈、深度链接 |
| 平台 | references/platform-handling.md | iOS/Android 代码、SafeArea、键盘 |
| 列表 | references/list-optimization.md | FlatList、性能、memo |
| 存储 | references/storage-hooks.md | AsyncStorage、MMKV、持久化 |
| 结构 | references/project-structure.md | 项目设置、架构 |
约束条件
必须做
- 列表使用 FlatList/SectionList(不用 ScrollView)
- 为列表项实现 memo + useCallback
- 处理 SafeAreaView 适配刘海屏
- 在 iOS 和 Android 真机上测试
- 表单使用 KeyboardAvoidingView
- 在导航中处理 Android 返回按钮
禁止做
- 对大列表使用 ScrollView
- 大量使用内联样式(会创建新对象)
- 硬编码尺寸(使用 Dimensions API 或 flex)
- 忽略订阅导致的内存泄漏
- 跳过平台特定测试
- 动画使用 waitFor/setTimeout(使用 Reanimated)
代码示例
使用 memo + useCallback 优化的 FlatList
import React, { memo, useCallback } from 'react';
import { FlatList, View, Text, StyleSheet } from 'react-native';
type Item = { id: string; title: string };
const ListItem = memo(({ title, onPress }: { title: string; onPress: () => void }) => (
<View style={styles.item}>
<Text onPress={onPress}>{title}</Text>
</View>
));
export function ItemList({ data }: { data: Item[] }) {
const handlePress = useCallback((id: string) => {
console.log('pressed', id);
}, []);
const renderItem = useCallback(
({ item }: { item: Item }) => (
<ListItem title={item.title} onPress={() => handlePress(item.id)} />
),
[handlePress]
);
return (
<FlatList
data={data}
keyExtractor={(item) => item.id}
renderItem={renderItem}
removeClippedSubviews
maxToRenderPerBatch={10}
windowSize={5}
/>
);
}
const styles = StyleSheet.create({
item: { padding: 16, borderBottomWidth: StyleSheet.hairlineWidth },
});
KeyboardAvoidingView 表单
import React from 'react';
import {
KeyboardAvoidingView,
Platform,
ScrollView,
TextInput,
StyleSheet,
SafeAreaView,
} from 'react-native';
export function LoginForm() {
return (
<SafeAreaView style={styles.safe}>
<KeyboardAvoidingView
style={styles.flex}
behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
>
<ScrollView contentContainerStyle={styles.content} keyboardShouldPersistTaps="handled">
<TextInput style={styles.input} placeholder="Email" autoCapitalize="none" />
<TextInput style={styles.input} placeholder="Password" secureTextEntry />
</ScrollView>
</KeyboardAvoidingView>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
safe: { flex: 1 },
flex: { flex: 1 },
content: { padding: 16, gap: 12 },
input: { borderWidth: 1, borderRadius: 8, padding: 12, fontSize: 16 },
});
平台特定组件
import { Platform, StyleSheet, View, Text } from 'react-native';
export function StatusChip({ label }: { label: string }) {
return (
<View style={styles.chip}>
<Text style={styles.label}>{label}</Text>
</View>
);
}
const styles = StyleSheet.create({
chip: {
paddingHorizontal: 12,
paddingVertical: 4,
borderRadius: 999,
backgroundColor: '#0a7ea4',
// 平台特定阴影
...Platform.select({
ios: { shadowColor: '#000', shadowOffset: { width: 0, height: 2 }, shadowOpacity: 0.2, shadowRadius: 4 },
android: { elevation: 3 },
}),
},
label: { color: '#fff', fontSize: 13, fontWeight: '600' },
});
输出格式
实现移动端功能时,提供:
- 组件结构(屏幕、导航、共享组件)
- 平台特定处理(iOS/Android 差异)
- 性能优化(memo、列表虚拟化)
- 样式方案(StyleSheet、主题)
- 架构决策的简要说明
兼容工具
Claude CodeCursor
标签
移动端
