当前端开发者想要进入移动端开发时,React Native 是最自然的选择—— 因为它让你用熟悉的 React 语法,开发真正的原生应用。
React Native 是什么?#
React Native 是 Facebook(现 Meta)于 2015 年发布的跨平台移动开发框架。它的核心特点是:使用 React 语法,开发真正的原生移动应用。
类比理解#
| 对比项 | Flutter | React Native |
|---|---|---|
| 渲染方式 | 自研引擎 | 调用原生组件 |
| 语言 | Dart | JavaScript/TypeScript |
| UI 一致性 | 完全一致 | 平台差异 |
| 前端友好度 | 需学新语言 | React 迁移 |
| 就像 | 自己开餐厅 | 加盟连锁店 |
Flutter 是"自己开餐厅"—— 从零打造一切。 React Native 是"加盟连锁店"—— 用别人的店,卖自己的东西。
核心特性#
1. 原生组件:调用真实 UI#
React Native 不自己画 UI,而是调用原生组件:
import { View, Text, Button, TextInput, FlatList, StyleSheet } from 'react-native';
function App() {
const [text, setText] = useState('');
const [items, setItems] = useState([]);
const addItem = () => {
if (text.trim()) {
setItems([...items, text]);
setText('');
}
};
return (
<View style={styles.container}>
<Text style={styles.title}>我的清单</Text>
<View style={styles.inputRow}>
<TextInput
style={styles.input}
value={text}
onChangeText={setText}
placeholder="添加项目..."
/>
<Button title="添加" onPress={addItem} />
</View>
<FlatList
data={items}
keyExtractor={(item, index) => index.toString()}
renderItem={({ item }) => (
<Text style={styles.item}>{item}</Text>
)}
/>
</View>
);
}
const styles = StyleSheet.create({
container: { flex: 1, padding: 20, paddingTop: 50 },
title: { fontSize: 24, fontWeight: 'bold', marginBottom: 20 },
inputRow: { flexDirection: 'row', marginBottom: 20 },
input: { flex: 1, borderWidth: 1, padding: 10, marginRight: 10 },
item: { padding: 15, borderBottomWidth: 1 },
});2. Bridge:JavaScript 与原生通信#
React Native 使用 Bridge 连接 JavaScript 和原生代码:
┌─────────────────┐ ┌─────────────────┐
│ JavaScript │ ←──→ │ Bridge │ ←──→ │ 原生代码 │
│ (React) │ │ (异步通信) │ │ (iOS/Android)│
└─────────────────┘ └─────────────────┘ └─────────────────┘3. JSX:类似 React 的语法#
React Native 使用类似 React 的 JSX 语法:
// React Native
<View>
<Text>Hello</Text>
<Button title="点击" onPress={handlePress} />
</View>
// Web React
<div>
<span>Hello</span>
<button onClick={handlePress}>点击</button>
</div>4. 常用组件#
React Native 提供了丰富的原生组件:
| 组件 | 作用 | 对应原生 |
|---|---|---|
| View | 容器 | UIView / android.view |
| Text | 文本 | UILabel / TextView |
| TextInput | 输入框 | UITextField / EditText |
| Button | 按钮 | UIButton / Button |
| Image | 图片 | UIImageView / ImageView |
| ScrollView | 滚动容器 | UIScrollView / ScrollView |
| FlatList | 列表 | UITableView / RecyclerView |
| TouchableOpacity | 可点击 | UIControl |
5. 样式系统:类似 CSS#
React Native 使用类似 CSS 的样式系统:
import { StyleSheet } from 'react-native';
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
title: {
fontSize: 24,
fontWeight: 'bold',
color: '#333',
marginBottom: 20,
},
button: {
backgroundColor: '#007AFF',
padding: 15,
borderRadius: 8,
},
});注意:React Native 使用 flex 布局,但不是所有 CSS 属性都支持。
6. 状态管理:React 生态#
React Native 可以使用 React 生态的状态管理方案:
// useState
const [count, setCount] = useState(0);
// useContext
const ThemeContext = createContext();
const theme = useContext(ThemeContext);
// Redux
import { Provider } from 'react-redux';
import { store } from './store';
function App() {
return (
<Provider store={store}>
<MainScreen />
</Provider>
);
}
// Zustand
import { create } from 'zustand';
const useStore = create(set => ({
count: 0,
increment: () => set(state => ({ count: state.count + 1 })),
}));7. 原生模块:扩展能力#
当需要原生能力时,可以编写原生模块:
// 调用原生模块
import { NativeModules, Platform } from 'react-native';
const { CalendarModule } = NativeModules;
// 使用
CalendarModule.createEvent('生日', '2024-01-01', (error: any) => {
if (error) console.error(error);
});为什么 React Native 如此流行?#
1. 前端开发者友好#
如果你会 React,学习 React Native 非常容易:
- 相同的组件化思想
- 相同的 JSX 语法
- 相同的 Hooks
2. 原生体验#
React Native 调用真实原生组件:
- 原生性能
- 原生 UI 外观
- 平台特性支持
3. 生态丰富#
React Native 拥有庞大的生态:
- 第三方组件库
- npm 包
- 社区支持
4. 大厂背书#
Facebook、Instagram、WhatsApp、Airbnb 等都在使用。
适用场景#
| 场景 | 适合程度 | 说明 |
|---|---|---|
| 跨平台 App | ⭐⭐⭐⭐⭐ | 核心场景 |
| 前端团队转型 | ⭐⭐⭐⭐⭐ | 学习成本低 |
| 快速 MVP | ⭐⭐⭐⭐ | 开发效率高 |
| 需要原生体验 | ⭐⭐⭐⭐ | 调用真实组件 |
| 复杂动画 | ⭐⭐⭐ | 性能略逊于 Flutter |
React Native 新架构(0.76+)#
React Native 0.76 引入了新架构:
| 特性 | 旧架构 | 新架构 |
|---|---|---|
| 通信方式 | Bridge(异步) | TurboModules(同步) |
| 渲染 | React 渲染器 | Fabric(同步) |
| 性能 | 中等 | 大幅提升 |
// 新架构下的原生组件
function MyComponent() {
return (
<View>
<Text>Hello New Architecture!</Text>
</View>
);
}学习路线建议#
入门阶段(2 周)#
- 掌握 React 基础
- 理解 React Native 组件
- 学会样式和布局(Flex)
- 掌握基本交互
进阶阶段(2-3 周)#
- 掌握导航(React Navigation)
- 学会状态管理
- 掌握网络请求
- 理解原生模块
高级阶段(持续学习)#
- 性能优化
- 原生开发
- 打包发布
- New Architecture
常用库#
| 库 | 用途 |
|---|---|
| React Navigation | 导航 |
| Redux / Zustand | 状态管理 |
| Axios | 网络请求 |
| react-native-vector-icons | 图标 |
| react-native-paper | UI 组件库 |
| react-native-reanimated | 动画 |
与 Flutter 对比#
| 特性 | React Native | Flutter |
|---|---|---|
| 语言 | JavaScript/TypeScript | Dart |
| 渲染 | 原生组件 | 自研引擎 |
| 性能 | ⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ |
| UI 一致性 | 平台差异 | 完全一致 |
| 前端友好 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐ |
| 生态 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐ |
| 学习成本 | 低 | 中等 |
总结#
React Native 为前端开发者打开了移动端的大门:
- React 语法—— 前端开发者零门槛
- 原生组件—— 获得原生体验
- 丰富生态—— 组件库丰富
- 大厂验证—— Facebook 背书
- 热重载—— 开发体验好
如果你会 React,想要快速进入移动端开发,React Native 是最佳选择。
写在最后
10 篇文章到此结束!我们覆盖了:
- 前端框架:React、Vue、Next.js、Astro
- 后端框架:Spring Boot、FastAPI、NestJS、Express
- 移动端框架:Flutter、React Native
希望这些文章能帮助你了解当前最流行的开发框架。每个框架都有自己的适用场景,选择时需要根据项目需求、团队技能和长期维护来综合考虑。
有问题欢迎评论区交流!
