React Native
React Native 上手教程
2026 年新建 React Native 项目,建议直接从 Expo + 新架构 开始。新架构从 0.76 起默认启用,0.82 起旧 Bridge 架构已被彻底移除,所以新教程不需要再学旧版 Bridge 写法。
一、先装环境
推荐准备:
- Node.js 20/22 LTS
- npm / yarn / pnpm
- VS Code
- 一台 Android 真机 或 iOS 模拟器
- 如果想用 Expo Go 快速预览,手机上安装 Expo Go
二、创建项目
终端执行:
npx create-expo-app@latest my-app
cd my-app
npx expo start启动后会出现一个二维码:
- Android 手机装好 Expo Go,扫码即可运行。
- iOS 可在局域网内扫码,或用模拟器运行。
如果想直接跑原生项目:
npx expo run:android
npx expo run:ios三、写第一个页面
打开 app/index.tsx,改成:
import { Text, View, StyleSheet } from "react-native";
export default function Home() {
return (
<View style={styles.container}>
<Text style={styles.title}>Hello, React Native</Text>
<Text style={styles.desc}>这是你的第一个 App 页面</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "#f5f5f5",
},
title: {
fontSize: 22,
fontWeight: "bold",
},
desc: {
marginTop: 8,
color: "#666",
},
});保存后 Metro 会自动刷新页面。
四、加一个跳转页面
安装文件路由:
npx expo install expo-router新建 app/details.tsx:
import { Text, View, StyleSheet } from "react-native";
export default function Details() {
return (
<View style={styles.container}>
<Text style={styles.title}>详情页</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
},
title: {
fontSize: 20,
fontWeight: "bold",
},
});在首页加跳转按钮:
import { Text, View, StyleSheet, Pressable } from "react-native";
import { Link } from "expo-router";
export default function Home() {
return (
<View style={styles.container}>
<Text style={styles.title}>Hello, React Native</Text>
<Link href="/details" asChild>
<Pressable style={styles.button}>
<Text style={styles.buttonText}>去详情页</Text>
</Pressable>
</Link>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "#f5f5f5",
gap: 16,
},
title: {
fontSize: 22,
fontWeight: "bold",
},
button: {
backgroundColor: "#007AFF",
paddingHorizontal: 20,
paddingVertical: 12,
borderRadius: 10,
},
buttonText: {
color: "#fff",
fontWeight: "600",
},
});五、写一个列表页
新建 app/news.tsx:
import { Text, View, FlatList, StyleSheet } from "react-native";
const NEWS = [
{ id: "1", title: "React Native 新架构已经全面普及" },
{ id: "2", title: "Expo Router 适合文件路由" },
{ id: "3", title: "Hermes 引擎提升启动速度" },
{ id: "4", title: "Fabric 渲染器让 UI 更流畅" },
];
export default function News() {
return (
<View style={styles.container}>
<FlatList
data={NEWS}
keyExtractor={(item) => item.id}
renderItem={({ item }) => (
<View style={styles.item}>
<Text style={styles.text}>{item.title}</Text>
</View>
)}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
},
item: {
padding: 16,
borderBottomWidth: 1,
borderBottomColor: "#eee",
},
text: {
fontSize: 16,
},
});六、常用技术选型
| 场景 | 推荐方案 |
|---|---|
| 路由 | Expo Router / React Navigation |
| 状态管理 | Zustand / Redux Toolkit |
| 网络请求 | axios + TanStack Query |
| 本地存储 | react-native-mmkv |
| 动画 | Reanimated + Gesture Handler |
| 列表 | FlashList / FlatList |
| 图标 | react-native-vector-icons |
| 热更新 | EAS Update / CodePush |
七、调试方式
- 在 Expo 菜单里打开 Reload、Performance Monitor、Element Inspector。
- 用 Flipper 查看 Hermes 调试、网络请求和日志。
- 用 React DevTools 查看组件树和状态变化。
- 真机调试优先用局域网连接,模拟器适合快速验证 UI。
八、打包发布
如果只是开发预览:
npx expo start如果要构建安装包:
npx eas build --platform android
npx eas build --platform iosEAS 会自动处理签名、构建和云端打包。生产发布前记得配置 App 名称、图标、权限和版本号。
学习路线
建议按这个顺序练:
- 用 Expo 跑通首页。
- 用
View / Text / Image / Pressable写静态页面。 - 用
FlatList做列表。 - 用 Expo Router 做页面跳转。
- 用
useState / useEffect管理状态。 - 接一个真实 API。
- 加图标、图片、权限。
- 用 EAS 打包到真机。