mcpField
R
react-native

react-native

github.com/claude-dev-suite开发4645 次安装5 天前更新
代码仓库

关于 react-native

面向 React Native 开发者的移动应用开发参考技能,涵盖界面、导航、网络请求、存储及发布准备等常见实践。

主要功能

  1. 01核心界面组件模式
  2. 02React Navigation 示例
  3. 03平台专属样式
  4. 04使用 TanStack Query 请求网络
  5. 05加密存储建议
  6. 06性能与发布检查清单

适用场景

  1. 01实现商品列表和屏幕导航
  2. 02获取数据并保存认证令牌
  3. 03检查性能与生产环境配置

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/claude-dev-suite/claude-dev-suite --skill react-native

安装后在 Claude Code 中输入/react-native确认已加载。

详细介绍

React Native

核心组件

import { View, Text, ScrollView, FlatList, Pressable, StyleSheet } from 'react-native';

function ProductList({ products }: { products: Product[] }) {
  return (
    <FlatList
      data={products}
      keyExtractor={(item) => item.id}
      renderItem={({ item }) => (
        <Pressable onPress={() => navigate('Detail', { id: item.id })} style={styles.card}>
          <Text style={styles.title}>{item.name}</Text>
          <Text style={styles.price}>${item.price}</Text>
        </Pressable>
      )}
      ItemSeparatorComponent={() => <View style={styles.separator} />}
    />
  );
}

const styles = StyleSheet.create({
  card: { padding: 16, backgroundColor: '#fff' },
  title: { fontSize: 16, fontWeight: '600' },
  price: { fontSize: 14, color: '#666', marginTop: 4 },
  separator: { height: 1, backgroundColor: '#eee' },
});

导航(React Navigation)

import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';

type RootStackParamList = {
  Home: undefined;
  Detail: { id: string };
};

const Stack = createNativeStackNavigator<RootStackParamList>();
const Tab = createBottomTabNavigator();

function App() {
  return (
    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen name="Home" component={HomeScreen} />
        <Stack.Screen name="Detail" component={DetailScreen} />
      </Stack.Navigator>
    </NavigationContainer>
  );
}

// Type-safe navigation
import { NativeStackScreenProps } from '@react-navigation/native-stack';
type DetailProps = NativeStackScreenProps<RootStackParamList, 'Detail'>;

function DetailScreen({ route, navigation }: DetailProps) {
  const { id } = route.params;
  return <Text>{id}</Text>;
}

平台特定代码

import { Platform } from 'react-native';

const styles = StyleSheet.create({
  shadow: Platform.select({
    ios: { shadowColor: '#000', shadowOffset: { width: 0, height: 2 }, shadowOpacity: 0.1 },
    android: { elevation: 3 },
  }),
});

// File-based: Component.ios.tsx / Component.android.tsx (auto-resolved)

网络

import { useQuery, useMutation } from '@tanstack/react-query';

function useProducts() {
  return useQuery({
    queryKey: ['products'],
    queryFn: async () => {
      const res = await fetch(`${API_URL}/products`);
      if (!res.ok) throw new Error('Failed to fetch');
      return res.json() as Promise<Product[]>;
    },
  });
}

安全存储

import EncryptedStorage from 'react-native-encrypted-storage';

await EncryptedStorage.setItem('auth_token', token);
const token = await EncryptedStorage.getItem('auth_token');
await EncryptedStorage.removeItem('auth_token');

性能

技术影响
列表使用 FlatList 而非 ScrollView高
在列表项上使用 React.memo中
在列表中使用 useCallback 处理事件中
Hermes 引擎(RN 0.70+ 中默认启用)高
避免在渲染时使用内联样式低至中
InteractionManager.runAfterInteractions中

反模式

反模式修复方法
长列表使用 ScrollView使用 FlatList 或 FlashList
FlatList 的 renderItem 中使用内联函数提取组件并使用 React.memo
将令牌存储在 AsyncStorage 中使用 react-native-encrypted-storage
没有错误边界在屏幕外层添加错误边界
忽略表单中的键盘使用 KeyboardAvoidingView

生产环境检查清单

  • 启用 Hermes 引擎
  • 为 Android 发布构建配置 ProGuard/R8
  • 配置应用签名(Android 密钥库、iOS 证书)
  • 配置深度链接
  • 错误跟踪(Sentry、Bugsnag)
  • OTA 更新(CodePush 或 Expo Updates)
  • 性能监控(Flipper、React DevTools)

评论

支持 Markdown