mobile
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseMobile Development
移动开发
Build cross-platform mobile applications.
构建跨平台移动应用。
When to Use
适用场景
- React Native development
- Flutter development
- Mobile performance issues
- Native module integration
- App store deployment
- React Native开发
- Flutter开发
- 移动性能问题排查
- 原生模块集成
- 应用商店部署
React Native
React Native
Component Structure
组件结构
tsx
import React, { useState, useCallback } from "react";
import { View, Text, TouchableOpacity, StyleSheet } from "react-native";
interface Props {
title: string;
onPress: () => void;
}
export function Button({ title, onPress }: Props) {
const [pressed, setPressed] = useState(false);
const handlePress = useCallback(() => {
setPressed(true);
onPress();
}, [onPress]);
return (
<TouchableOpacity
style={[styles.button, pressed && styles.pressed]}
onPress={handlePress}
activeOpacity={0.7}
>
<Text style={styles.text}>{title}</Text>
</TouchableOpacity>
);
}
const styles = StyleSheet.create({
button: {
backgroundColor: "#007AFF",
padding: 16,
borderRadius: 8,
},
pressed: {
opacity: 0.8,
},
text: {
color: "white",
fontWeight: "600",
textAlign: "center",
},
});tsx
import React, { useState, useCallback } from "react";
import { View, Text, TouchableOpacity, StyleSheet } from "react-native";
interface Props {
title: string;
onPress: () => void;
}
export function Button({ title, onPress }: Props) {
const [pressed, setPressed] = useState(false);
const handlePress = useCallback(() => {
setPressed(true);
onPress();
}, [onPress]);
return (
<TouchableOpacity
style={[styles.button, pressed && styles.pressed]}
onPress={handlePress}
activeOpacity={0.7}
>
<Text style={styles.text}>{title}</Text>
</TouchableOpacity>
);
}
const styles = StyleSheet.create({
button: {
backgroundColor: "#007AFF",
padding: 16,
borderRadius: 8,
},
pressed: {
opacity: 0.8,
},
text: {
color: "white",
fontWeight: "600",
textAlign: "center",
},
});Navigation
导航实现
tsx
import { NavigationContainer } from "@react-navigation/native";
import { createNativeStackNavigator } from "@react-navigation/native-stack";
type RootStackParamList = {
Home: undefined;
Details: { id: string };
};
const Stack = createNativeStackNavigator<RootStackParamList>();
function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Details" component={DetailsScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}tsx
import { NavigationContainer } from "@react-navigation/native";
import { createNativeStackNavigator } from "@react-navigation/native-stack";
type RootStackParamList = {
Home: undefined;
Details: { id: string };
};
const Stack = createNativeStackNavigator<RootStackParamList>();
function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Details" component={DetailsScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}Flutter
Flutter
Widget Structure
组件结构
dart
class MyButton extends StatelessWidget {
final String title;
final VoidCallback onPressed;
const MyButton({
Key? key,
required this.title,
required this.onPressed,
}) : super(key: key);
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: onPressed,
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.all(16),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
child: Text(title),
);
}
}dart
class MyButton extends StatelessWidget {
final String title;
final VoidCallback onPressed;
const MyButton({
Key? key,
required this.title,
required this.onPressed,
}) : super(key: key);
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: onPressed,
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.all(16),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
child: Text(title),
);
}
}Performance Tips
性能优化技巧
- Use FlatList/ListView for long lists
- Memoize callbacks with useCallback
- Avoid inline styles (use StyleSheet)
- Lazy load screens and images
- Profile with Flipper/DevTools
- 使用FlatList/ListView处理长列表
- 用useCallback缓存回调函数
- 避免内联样式(使用StyleSheet)
- 懒加载页面和图片
- 使用Flipper/DevTools进行性能分析
Common Patterns
常见模式
| Pattern | React Native | Flutter |
|---|---|---|
| State | useState/Redux | setState/Riverpod |
| Navigation | React Navigation | Navigator 2.0 |
| HTTP | fetch/axios | http/dio |
| Storage | AsyncStorage | shared_preferences |
| Animations | Animated/Reanimated | AnimationController |
| 模式 | React Native | Flutter |
|---|---|---|
| 状态管理 | useState/Redux | setState/Riverpod |
| 导航 | React Navigation | Navigator 2.0 |
| HTTP请求 | fetch/axios | http/dio |
| 本地存储 | AsyncStorage | shared_preferences |
| 动画实现 | Animated/Reanimated | AnimationController |
Examples
示例
Input: "Build a list screen"
Action: Create FlatList with virtualization, pull-to-refresh, pagination
Input: "Add offline support"
Action: Implement AsyncStorage caching, sync queue, network detection
输入: "构建一个列表页面"
操作: 创建带有虚拟化、下拉刷新、分页功能的FlatList
输入: "添加离线支持"
操作: 实现AsyncStorage缓存、同步队列、网络检测功能