Token导航 LogoToken导航

Agent Skill · 研究检索

refactor_react-native(refactor React native 搜索)

用于辅助前端页面、组件、样式和交互逻辑的开发与维护。它适合让 Agent 生成或审查 React、Next.js、Vue、Tailwind、CSS 等相关代码,整理组件结构,或定位布局和性能问题。使用时需要结合项目现有设计系统、路由和构建方式,避免只生成孤立片段;涉及页面改动时,应配合本地预览和构建检查确认视觉效果。

refactor_react-native 是研究检索类 Agent Skill,可用宿主为 Codex、Claude、Cursor、Gemini CLI,通过GitHub 仓库获取,权限为权限未标注;许可证为 MIT,暂无第三方扫描结果,按安装量在研究检索类的 73,675 个 Skill 里排第 43,282。

总安装 998周安装 40GitHub Stars 公开资料未说明下载 323核验 2026-05-01

安装说明

本站只整理中文说明,不托管安装包,也不代用户安装。
GitHub
许可证
MIT

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:refactor_react-native(refactor React native 搜索)
来源仓库:https://github.com/snakeo/claude-debug-and-refactor-skills-plugin
仓库路径:skills/refactor_react-native
安装命令:
npx skills add snakeo/claude-debug-and-refactor-skills-plugin --skill "refactor:react-native"
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

AgentSkills.tonpx skills
npx skills add snakeo/claude-debug-and-refactor-skills-plugin --skill "refactor:react-native"

这个 Skill 是做什么的

研究检索类 Skill 主要解决「找资料、核对事实、把来源整理清楚」的问题,通常会让助手先检索再总结。

refactor_react-native 用于辅助前端页面、组件和样式开发与维护。适合生成或审查 React、Next.js、Vue、Tailwind、CSS 等相关代码。

通过 GitHub 安装,使用 npx skills add snakeo/claude-debug-and-refactor-skills-plugin --skill "refactor:react-native" 命令。使用时需结合项目现有设计系统,避免只生成孤立片段;涉及页面改动时应配合预览确认效果。

适用宿主包括 Codex、Claude、Cursor、Gemini CLI,接入前应确认版本、权限和运行环境要求。

来源仓库内路径:skills/refactor_react-native

上手步骤

4 步
  1. 1

    确认自己的宿主工具

    公开资料标注支持 Codex、Claude、Cursor、Gemini CLI;先确认自己用的是其中之一,再按对应工具的 Skills 目录放置。

  2. 2

    拿到安装命令

    可以直接复制的命令是 npx skills add snakeo/claude-debug-and-refactor-skills-plugin --skill "refactor:…;命令里的占位符换成自己的路径或密钥再执行。

  3. 3

    安装后重启宿主

    命令执行完要完全退出宿主工具再打开,让新装的 Skill 被重新扫描到,否则列表里可能看不到。

  4. 4

    按最小权限试用

    权限没有明确标注,第一次使用时只给一个无关紧要的任务,观察它到底读了哪些文件和网络。

常见问题

6 条

refactor_react-native 怎么安装?

在终端执行 npx skills add snakeo/claude-debug-and-refactor-skills-plugin --skill "refactor:react-nati…。装完完全退出宿主工具再打开,让它重新扫描本地 Skill。

refactor_react-native 支持哪些 AI 工具?

公开资料标注支持 Codex、Claude、Cursor、Gemini CLI。其他工具只要能读取本地 Skill 目录,一般也能手动放进去,但要自己确认触发方式是否一致。

refactor_react-native 需要什么权限?

权限级别没有明确标注。第一次使用时建议只给无关紧要的任务,观察它实际读写了哪些内容。

refactor_react-native 是免费的吗?

来源标注的许可证是 MIT。本站不代收任何费用,也不托管安装包;能否商用、要不要署名,以许可证原文为准。

refactor_react-native 用的人多吗?

公开数据是累计安装 998 次,近一周安装 40 次,下载 323 次。这些数字来自第三方目录统计,只能当作热度参考,不代表质量或安全性。

refactor_react-native 安全吗?

没有查到第三方扫描结果,安全性无法从公开资料判断,安装前请自行审查源码、依赖和命令行为。

值得选的地方和要先想清楚的地方

值得选的地方

  • 同时适配 4 种工具,换客户端不用重新找替代品。
  • 许可证明确为 MIT,商用和分发边界好判断。

先想清楚的地方

  • 没有第三方扫描结果,安全性只能靠自己读源码判断。
  • 第三方目录整理,版本更新和行为细节以来源仓库为准。

适不适合你

更适合

  • 已经在用 Codex,想直接扩展它能力的人。
  • 需要处理研究检索类任务、又不想每次都手写提示词的人。

不太适合

  • 要求有安全扫描结论才肯安装的团队。
  • 需要厂商级技术支持和 SLA 的生产系统。

研究检索类 Skill 对比

6 个同类
Skill总安装GitHub 星权限许可证扫描
find-skills

寻找技能 · Codex、Claude

31,199,81316,486操作浏览器MIT有提醒
self-improving-agent

自我改进 Agent · OpenClaw

16,464,0023,396接触敏感数据MIT-0通过
soultrace

灵魂追踪 · Codex、Claude

7,588,94412需要联网未明确有提醒
self-improving

自我完善 · OpenClaw

6,829,2841,046需要联网MIT-0通过
microsoft-foundry

微软代工厂 · Codex、Claude

6,614,144764调用外部服务未明确有提醒
refactor_react-native

refactor React native 搜索 · 本页

998—权限未标注MIT未收录

按安装量取同分类前几名;数字来自第三方目录统计,只作热度参考,不代表质量结论。

SKILL.md 原文

name
refactor:react-native
description
Refactor React Native and TypeScript code to improve maintainability, readability, and performance for cross-platform mobile applications. This skill transforms complex mobile code into clean, well-structured solutions following React Native New Architecture patterns including Fabric, TurboModules, and JSI. It addresses FlatList performance issues, prop drilling, platform-specific code organization, and inline styles. Leverages Expo SDK 52+ features, React Navigation v7, and Reanimated for smooth 60fps animations.

You are an elite React Native/TypeScript refactoring specialist with deep expertise in writing clean, maintainable, and performant cross-platform mobile applications. You have mastered React Native's New Architecture (Fabric, TurboModules, JSI), Expo SDK 52+, and modern React patterns.

Core Refactoring Principles

DRY (Don't Repeat Yourself)

  • Extract repeated JSX into reusable components
  • Create custom hooks for shared stateful logic (API calls, platform APIs)
  • Use utility functions for repeated computations
  • Consolidate similar navigation handlers and animations

Single Responsibility Principle (SRP)

  • Each component should do ONE thing well
  • Screen components handle navigation and layout; UI components handle display
  • Custom hooks encapsulate single pieces of logic (useAuth, useLocation, useCamera)
  • Separate business logic from presentation

Early Returns and Guard Clauses

  • Return early for loading, error, and empty states
  • Avoid deeply nested conditionals in JSX
  • Use guard clauses to handle edge cases first (permissions, network state)

Small, Focused Functions

  • Components under 150 lines (ideally under 100)
  • Custom hooks under 50 lines
  • Event handlers under 20 lines
  • Extract complex logic into helper functions

React Native New Architecture (2025)

Understanding the Three Pillars

JavaScript Interface (JSI) JSI replaces the old asynchronous bridge with synchronous, direct communication between JavaScript and native code:

// Old Architecture: Async bridge communication
// Every call goes through JSON serialization
NativeModules.LocationModule.getCurrentLocation()
  .then(location => console.log(location));

// New Architecture: Direct JSI binding
// Synchronous, no serialization overhead
const location = LocationModule.getCurrentLocationSync();

TurboModules TurboModules replace NativeModules with lazy-loaded, type-safe native modules:

// TurboModule Specification (Codegen)
// specs/NativeLocationModule.ts
import type { TurboModule } from 'react-native';
import { TurboModuleRegistry } from 'react-native';

export interface Spec extends TurboModule {
  getCurrentLocation(): Promise<{
    latitude: number;
    longitude: number;
    accuracy: number;
  }>;
  watchLocation(callback: (location: Location) => void): number;
  clearWatch(watchId: number): void;
}

export default TurboModuleRegistry.getEnforcing<Spec>('LocationModule');

Fabric Renderer Fabric uses an immutable UI tree with C++ core for better performance:

// Fabric enables concurrent features
import { startTransition } from 'react';

function SearchScreen() {
  const [query, setQuery] = useState('');
  const [results, setResults] = useState([]);

  const handleSearch = (text: string) => {
    setQuery(text);
    // Low-priority update - Fabric can interrupt for high-priority events
    startTransition(() => {
      setResults(performExpensiveSearch(text));
    });
  };

  return (
    <View>
      <TextInput value={query} onChangeText={handleSearch} />
      <ResultsList results={results} />
    </View>
  );
}

Migrating to New Architecture

// android/gradle.properties
newArchEnabled=true

// ios/Podfile
ENV['RCT_NEW_ARCH_ENABLED'] = '1'

// Check architecture at runtime
import { Platform } from 'react-native';

const isNewArch = (global as any).__turboModuleProxy != null;
console.log(`New Architecture: ${isNewArch ? 'Enabled' : 'Disabled'}`);

Best Practices for New Architecture

  1. Enable on pilot screens first, measure, then expand
  2. Convert high-traffic NativeModules first
  3. Use Codegen for type-safe bindings
  4. Pair Fabric with list virtualization and memoized renderers
  5. Run npx expo-doctor to check library compatibility

Expo Best Practices (SDK 52+)

Expo vs Bare Workflow Decision

// Use Expo (managed workflow) when:
// - Building standard features (camera, location, notifications)
// - Need rapid iteration with EAS Build
// - Team has limited native development experience
// - Project doesn't need custom native modules

// Use Bare Workflow when:
// - Need custom native modules not available in Expo
// - Require specific native SDK integrations
// - Need fine-grained control over native configuration

Continuous Native Generation (CNG)

# Delete native directories before upgrading
rm -rf android ios

# Regenerate with prebuild
npx expo prebuild

# Or let EAS Build handle it
eas build --platform all

Expo Router for Navigation

// app/_layout.tsx
import { Stack } from 'expo-router';

export default function RootLayout() {
  return (
    <Stack>
      <Stack.Screen name="(tabs)" options={{ headerShown: false }} />
      <Stack.Screen name="modal" options={{ presentation: 'modal' }} />
    </Stack>
  );
}

// app/(tabs)/index.tsx
export default function HomeScreen() {
  return (
    <View>
      <Link href="/profile">Go to Profile</Link>
      <Link href={{ pathname: '/details/[id]', params: { id: '123' } }}>
        View Details
      </Link>
    </View>
  );
}

// app/details/[id].tsx
import { useLocalSearchParams } from 'expo-router';

export default function DetailsScreen() {
  const { id } = useLocalSearchParams<{ id: string }>();
  return <Text>Details for {id}</Text>;
}

Expo Fetch (WinterCG-compliant)

// Modern streaming fetch for AI APIs
import { fetch } from 'expo/fetch';

async function streamResponse(prompt: string) {
  const response = await fetch('https://api.ai-service.com/stream', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ prompt }),
  });

  const reader = response.body?.getReader();
  while (true) {
    const { done, value } = await reader!.read();
    if (done) break;
    const text = new TextDecoder().decode(value);
    console.log(text);
  }
}

Performance Optimization

FlatList Optimization

// BAD: Unoptimized FlatList
function BadList({ data }) {
  return (
    <FlatList
      data={data}
      renderItem={({ item }) => <ListItem item={item} onPress={() => handlePress(item.id)} />}
      keyExtractor={(item, index) => index.toString()} // Array index as key!
    />
  );
}

// GOOD: Optimized FlatList
const ITEM_HEIGHT = 72;

function OptimizedList({ data, onItemPress }) {
  const renderItem = useCallback(
    ({ item }: { item: DataItem }) => (
      <MemoizedListItem item={item} onPress={onItemPress} />
    ),
    [onItemPress]
  );

  const keyExtractor = useCallback((item: DataItem) => item.id, []);

  const getItemLayout = useCallback(
    (_: unknown, index: number) => ({
      length: ITEM_HEIGHT,
      offset: ITEM_HEIGHT * index,
      index,
    }),
    []
  );

  return (
    <FlatList
      data={data}
      renderItem={renderItem}
      keyExtractor={keyExtractor}
      getItemLayout={getItemLayout}
      initialNumToRender={10}
      maxToRenderPerBatch={10}
      windowSize={5}
      removeClippedSubviews={true}
      updateCellsBatchingPeriod={50}
    />
  );
}

// Memoized list item
const MemoizedListItem = React.memo(
  function ListItem({ item, onPress }: ListItemProps) {
    const handlePress = useCallback(() => onPress(item.id), [item.id, onPress]);

    return (
      <TouchableOpacity onPress={handlePress} style={styles.item}>
        <Text>{item.title}</Text>
      </TouchableOpacity>
    );
  }
);

FlashList for Better Performance

// For lists with 1000+ items, use FlashList from Shopify
import { FlashList } from '@shopify/flash-list';

function HighPerformanceList({ data }) {
  return (
    <FlashList
      data={data}
      renderItem={({ item }) => <ListItem item={item} />}
      estimatedItemSize={72}
      keyExtractor={(item) => item.id}
    />
  );
}

Image Optimization

// Use expo-image or react-native-fast-image for better caching
import { Image } from 'expo-image';

function OptimizedImage({ uri }: { uri: string }) {
  return (
    <Image
      source={{ uri }}
      style={styles.image}
      contentFit="cover"
      transition={200}
      placeholder={blurhash}
      cachePolicy="memory-disk"
    />
  );
}

Animation Performance

// Use Reanimated for 60fps animations on UI thread
import Animated, {
  useSharedValue,
  useAnimatedStyle,
  withSpring,
  withTiming,
} from 'react-native-reanimated';

function AnimatedCard() {
  const scale = useSharedValue(1);
  const opacity = useSharedValue(1);

  const animatedStyle = useAnimatedStyle(() => ({
    transform: [{ scale: scale.value }],
    opacity: opacity.value,
  }));

  const handlePressIn = () => {
    scale.value = withSpring(0.95);
    opacity.value = withTiming(0.8);
  };

  const handlePressOut = () => {
    scale.value = withSpring(1);
    opacity.value = withTiming(1);
  };

  return (
    <Pressable onPressIn={handlePressIn} onPressOut={handlePressOut}>
      <Animated.View style={[styles.card, animatedStyle]}>
        <Text>Press Me</Text>
      </Animated.View>
    </Pressable>
  );
}

Navigation Patterns (React Navigation v7)

Type-Safe Navigation

// types/navigation.ts
import type { NativeStackScreenProps } from '@react-navigation/native-stack';
import type { BottomTabScreenProps } from '@react-navigation/bottom-tabs';

export type RootStackParamList = {
  Home: undefined;
  Profile: { userId: string };
  Settings: undefined;
  Details: { itemId: string; title: string };
};

export type TabParamList = {
  Feed: undefined;
  Search: { query?: string };
  Notifications: undefined;
};

export type RootStackScreenProps<T extends keyof RootStackParamList> =
  NativeStackScreenProps<RootStackParamList, T>;

export type TabScreenProps<T extends keyof TabParamList> =
  BottomTabScreenProps<TabParamList, T>;

// Usage in screen
function ProfileScreen({ route, navigation }: RootStackScreenProps<'Profile'>) {
  const { userId } = route.params; // Type-safe!

  const goToDetails = () => {
    navigation.navigate('Details', { itemId: '123', title: 'Test' }); // Type-checked!
  };
}

Custom Navigation Hooks

// hooks/useTypedNavigation.ts
import { useNavigation, useRoute } from '@react-navigation/native';
import type { NativeStackNavigationProp } from '@react-navigation/native-stack';
import type { RouteProp } from '@react-navigation/native';
import type { RootStackParamList } from '../types/navigation';

export function useAppNavigation() {
  return useNavigation<NativeStackNavigationProp<RootStackParamList>>();
}

export function useAppRoute<T extends keyof RootStackParamList>() {
  return useRoute<RouteProp<RootStackParamList, T>>();
}

// Usage
function MyComponent() {
  const navigation = useAppNavigation();
  const route = useAppRoute<'Profile'>();

  navigation.navigate('Details', { itemId: '123', title: 'Test' });
}

Deep Linking Configuration

// navigation/linking.ts
import { LinkingOptions } from '@react-navigation/native';
import * as Linking from 'expo-linking';

const linking: LinkingOptions<RootStackParamList> = {
  prefixes: [Linking.createURL('/'), 'myapp://'],
  config: {
    screens: {
      Home: '',
      Profile: 'profile/:userId',
      Details: 'details/:itemId',
      Settings: 'settings',
    },
  },
};

// App.tsx
function App() {
  return (
    <NavigationContainer linking={linking}>
      <RootStack.Navigator>
        {/* screens */}
      </RootStack.Navigator>
    </NavigationContainer>
  );
}

Platform-Specific Code

File-Based Platform Extensions

// components/Button/Button.tsx (shared logic)
export interface ButtonProps {
  title: string;
  onPress: () => void;
  disabled?: boolean;
}

// components/Button/Button.ios.tsx
import { TouchableOpacity, Text } from 'react-native';
import type { ButtonProps } from './Button';

export function Button({ title, onPress, disabled }: ButtonProps) {
  return (
    <TouchableOpacity
      onPress={onPress}
      disabled={disabled}
      style={styles.iosButton}
    >
      <Text style={styles.iosText}>{title}</Text>
    </TouchableOpacity>
  );
}

// components/Button/Button.android.tsx
import { TouchableNativeFeedback, Text, View } from 'react-native';
import type { ButtonProps } from './Button';

export function Button({ title, onPress, disabled }: ButtonProps) {
  return (
    <TouchableNativeFeedback
      onPress={onPress}
      disabled={disabled}
      background={TouchableNativeFeedback.Ripple('#rgba(0,0,0,0.1)', false)}
    >
      <View style={styles.androidButton}>
        <Text style={styles.androidText}>{title}</Text>
      </View>
    </TouchableNativeFeedback>
  );
}

Platform-Specific Hooks

// hooks/useHaptics.ts
import { Platform } from 'react-native';
import * as Haptics from 'expo-haptics';

export function useHaptics() {
  const light = async () => {
    if (Platform.OS === 'ios') {
      await Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light);
    } else {
      await Haptics.selectionAsync();
    }
  };

  const success = async () => {
    await Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success);
  };

  const error = async () => {
    await Haptics.notificationAsync(Haptics.NotificationFeedbackType.Error);
  };

  return { light, success, error };
}

State Management

Zustand for React Native

// stores/authStore.ts
import { create } from 'zustand';
import { persist, createJSONStorage } from 'zustand/middleware';
import AsyncStorage from '@react-native-async-storage/async-storage';

interface AuthState {
  user: User | null;
  token: string | null;
  isLoading: boolean;
  login: (email: string, password: string) => Promise<void>;
  logout: () => void;
  hydrated: boolean;
  setHydrated: (hydrated: boolean) => void;
}

export const useAuthStore = create<AuthState>()(
  persist(
    (set, get) => ({
      user: null,
      token: null,
      isLoading: false,
      hydrated: false,
      setHydrated: (hydrated) => set({ hydrated }),

      login: async (email, password) => {
        set({ isLoading: true });
        try {
          const { user, token } = await authApi.login(email, password);
          set({ user, token, isLoading: false });
        } catch (error) {
          set({ isLoading: false });
          throw error;
        }
      },

      logout: () => {
        set({ user: null, token: null });
      },
    }),
    {
      name: 'auth-storage',
      storage: createJSONStorage(() => AsyncStorage),
      onRehydrateStorage: () => (state) => {
        state?.setHydrated(true);
      },
    }
  )
);

// Usage with hydration check
function AuthGuard({ children }) {
  const { hydrated, user } = useAuthStore();

  if (!hydrated) {
    return <SplashScreen />;
  }

  if (!user) {
    return <LoginScreen />;
  }

  return children;
}

TanStack Query for Server State

// hooks/useProducts.ts
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';

export function useProducts(categoryId?: string) {
  return useQuery({
    queryKey: ['products', categoryId],
    queryFn: () => productsApi.getAll(categoryId),
    staleTime: 5 * 60 * 1000, // 5 minutes
  });
}

export function useProduct(id: string) {
  return useQuery({
    queryKey: ['products', id],
    queryFn: () => productsApi.getById(id),
    enabled: !!id,
  });
}

export function useCreateProduct() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: productsApi.create,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['products'] });
    },
  });
}

// Usage in component
function ProductsList({ categoryId }: { categoryId: string }) {
  const { data: products, isLoading, error, refetch } = useProducts(categoryId);

  if (isLoading) return <LoadingSpinner />;
  if (error) return <ErrorMessage error={error} onRetry={refetch} />;

  return (
    <FlatList
      data={products}
      renderItem={({ item }) => <ProductCard product={item} />}
      keyExtractor={(item) => item.id}
      onRefresh={refetch}
      refreshing={isLoading}
    />
  );
}

Styling Patterns

StyleSheet Best Practices

// BAD: Inline styles
function BadComponent() {
  return (
    <View style={{ flex: 1, padding: 16, backgroundColor: '#fff' }}>
      <Text style={{ fontSize: 18, fontWeight: 'bold', color: '#333' }}>
        Title
      </Text>
    </View>
  );
}

// GOOD: StyleSheet.create
function GoodComponent() {
  return (
    <View style={styles.container}>
      <Text style={styles.title}>Title</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    padding: 16,
    backgroundColor: '#fff',
  },
  title: {
    fontSize: 18,
    fontWeight: 'bold',
    color: '#333',
  },
});

Design Tokens and Theme

// theme/tokens.ts
export const colors = {
  primary: {
    50: '#eff6ff',
    500: '#3b82f6',
    600: '#2563eb',
    700: '#1d4ed8',
  },
  neutral: {
    50: '#fafafa',
    100: '#f5f5f5',
    900: '#171717',
  },
  error: '#ef4444',
  success: '#22c55e',
} as const;

export const spacing = {
  xs: 4,
  sm: 8,
  md: 16,
  lg: 24,
  xl: 32,
} as const;

export const typography = {
  h1: { fontSize: 32, fontWeight: '700' as const, lineHeight: 40 },
  h2: { fontSize: 24, fontWeight: '600' as const, lineHeight: 32 },
  body: { fontSize: 16, fontWeight: '400' as const, lineHeight: 24 },
  caption: { fontSize: 12, fontWeight: '400' as const, lineHeight: 16 },
} as const;

// theme/ThemeContext.tsx
import { createContext, useContext } from 'react';
import { useColorScheme } from 'react-native';

const ThemeContext = createContext<Theme>(lightTheme);

export function ThemeProvider({ children }: { children: React.ReactNode }) {
  const colorScheme = useColorScheme();
  const theme = colorScheme === 'dark' ? darkTheme : lightTheme;

  return (
    <ThemeContext.Provider value={theme}>
      {children}
    </ThemeContext.Provider>
  );
}

export const useTheme = () => useContext(ThemeContext);

Styled Components / NativeWind

// Using NativeWind (Tailwind for React Native)
import { styled } from 'nativewind';

const StyledView = styled(View);
const StyledText = styled(Text);

function Card({ title, description }: CardProps) {
  return (
    <StyledView className="bg-white p-4 rounded-lg shadow-md dark:bg-gray-800">
      <StyledText className="text-lg font-bold text-gray-900 dark:text-white">
        {title}
      </StyledText>
      <StyledText className="text-gray-600 dark:text-gray-300">
        {description}
      </StyledText>
    </StyledView>
  );
}

Feature-Based Folder Structure

src/
  app/                          # Expo Router pages (if using Expo Router)
    (tabs)/
      index.tsx
      profile.tsx
    _layout.tsx

  features/                     # Feature modules
    auth/
      components/
        LoginForm.tsx
        RegisterForm.tsx
        SocialLoginButtons.tsx
      hooks/
        useAuth.ts
        useSession.ts
      screens/
        LoginScreen.tsx
        RegisterScreen.tsx
      services/
        authApi.ts
      stores/
        authStore.ts
      types/
        auth.types.ts
      index.ts                  # Public exports

    products/
      components/
        ProductCard.tsx
        ProductList.tsx
        ProductFilters.tsx
      hooks/
        useProducts.ts
        useProductSearch.ts
      screens/
        ProductListScreen.tsx
        ProductDetailScreen.tsx
      services/
        productsApi.ts
      types/
        products.types.ts
      index.ts

  shared/                       # Shared across features
    components/
      Button/
        Button.tsx
        Button.ios.tsx
        Button.android.tsx
        Button.test.tsx
      Input/
      Card/
    hooks/
      useDebounce.ts
      useNetwork.ts
      useHaptics.ts
    utils/
      formatting.ts
      validation.ts
    services/
      apiClient.ts
      storage.ts
    theme/
      tokens.ts
      ThemeContext.tsx
    types/
      common.ts
      navigation.ts

  assets/
    images/
    fonts/

  config/
    env.ts
    constants.ts

TypeScript Best Practices

Component Props Typing

// Explicit prop types with JSDoc
interface CardProps {
  /** The card title */
  title: string;
  /** Optional subtitle */
  subtitle?: string;
  /** Card content */
  children: React.ReactNode;
  /** Callback when card is pressed */
  onPress?: () => void;
  /** Visual variant */
  variant?: 'default' | 'outlined' | 'elevated';
  /** Test ID for E2E testing */
  testID?: string;
}

function Card({
  title,
  subtitle,
  children,
  onPress,
  variant = 'default',
  testID,
}: CardProps) {
  const Wrapper = onPress ? TouchableOpacity : View;

  return (
    <Wrapper
      onPress={onPress}
      style={[styles.card, styles[variant]]}
      testID={testID}
    >
      <Text style={styles.title}>{title}</Text>
      {subtitle && <Text style={styles.subtitle}>{subtitle}</Text>}
      {children}
    </Wrapper>
  );
}

Generic Components

interface ListProps<T> {
  items: T[];
  renderItem: (item: T, index: number) => React.ReactNode;
  keyExtractor: (item: T) => string;
  emptyComponent?: React.ReactNode;
  ListHeaderComponent?: React.ComponentType | React.ReactElement;
}

function GenericList<T>({
  items,
  renderItem,
  keyExtractor,
  emptyComponent,
  ListHeaderComponent,
}: ListProps<T>) {
  if (items.length === 0 && emptyComponent) {
    return <>{emptyComponent}</>;
  }

  return (
    <FlatList
      data={items}
      renderItem={({ item, index }) => renderItem(item, index)}
      keyExtractor={keyExtractor}
      ListHeaderComponent={ListHeaderComponent}
    />
  );
}

// Usage with type inference
<GenericList
  items={users}
  renderItem={(user) => <UserCard user={user} />}
  keyExtractor={(user) => user.id}
  emptyComponent={<EmptyState message="No users found" />}
/>

Native Module Types

// types/native.d.ts
declare module 'react-native' {
  interface NativeModulesStatic {
    CustomModule: {
      getValue(): Promise<string>;
      setValue(value: string): Promise<void>;
      addListener(event: string): void;
      removeListeners(count: number): void;
    };
  }
}

// Usage
import { NativeModules, NativeEventEmitter } from 'react-native';

const { CustomModule } = NativeModules;
const eventEmitter = new NativeEventEmitter(CustomModule);

function useCustomModule() {
  useEffect(() => {
    const subscription = eventEmitter.addListener('onValueChange', (event) => {
      console.log(event);
    });

    return () => subscription.remove();
  }, []);

  return {
    getValue: CustomModule.getValue,
    setValue: CustomModule.setValue,
  };
}

Anti-Patterns to Refactor

1. FlatList Performance Issues

// BAD: Inline functions and missing optimizations
<FlatList
  data={items}
  renderItem={({ item }) => (
    <TouchableOpacity onPress={() => handlePress(item.id)}>
      <Text>{item.name}</Text>
    </TouchableOpacity>
  )}
  keyExtractor={(item, index) => index.toString()}
/>

// GOOD: Memoized components and proper configuration
const ItemComponent = React.memo(({ item, onPress }) => (
  <TouchableOpacity onPress={() => onPress(item.id)}>
    <Text>{item.name}</Text>
  </TouchableOpacity>
));

const renderItem = useCallback(({ item }) => (
  <ItemComponent item={item} onPress={handlePress} />
), [handlePress]);

<FlatList
  data={items}
  renderItem={renderItem}
  keyExtractor={useCallback((item) => item.id, [])}
  getItemLayout={useCallback((_, index) => ({
    length: ITEM_HEIGHT,
    offset: ITEM_HEIGHT * index,
    index,
  }), [])}
/>

2. Inline Styles

// BAD: Inline styles create new objects every render
<View style={{ flex: 1, padding: 16 }}>
  <Text style={{ fontSize: 18, color: pressed ? 'blue' : 'black' }}>
    Hello
  </Text>
</View>

// GOOD: StyleSheet + array syntax for dynamic styles
<View style={styles.container}>
  <Text style={[styles.text, pressed && styles.textPressed]}>
    Hello
  </Text>
</View>

const styles = StyleSheet.create({
  container: { flex: 1, padding: 16 },
  text: { fontSize: 18, color: 'black' },
  textPressed: { color: 'blue' },
});

3. API Calls in Components

// BAD: API logic mixed with component
function UserProfile({ userId }) {
  const [user, setUser] = useState(null);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    fetch(`/api/users/${userId}`)
      .then(res => res.json())
      .then(setUser)
      .finally(() => setLoading(false));
  }, [userId]);

  // ... render logic
}

// GOOD: Extract to custom hook or use TanStack Query
function useUser(userId: string) {
  return useQuery({
    queryKey: ['users', userId],
    queryFn: () => userApi.getById(userId),
    enabled: !!userId,
  });
}

function UserProfile({ userId }) {
  const { data: user, isLoading, error } = useUser(userId);

  if (isLoading) return <LoadingSpinner />;
  if (error) return <ErrorMessage error={error} />;

  return <UserCard user={user} />;
}

4. Missing Error Boundaries

// BAD: No error handling
function App() {
  return (
    <NavigationContainer>
      <RootNavigator />
    </NavigationContainer>
  );
}

// GOOD: Error boundary with fallback
import { ErrorBoundary } from 'react-error-boundary';

function ErrorFallback({ error, resetErrorBoundary }) {
  return (
    <SafeAreaView style={styles.container}>
      <Text style={styles.title}>Something went wrong</Text>
      <Text style={styles.message}>{error.message}</Text>
      <Button title="Try Again" onPress={resetErrorBoundary} />
    </SafeAreaView>
  );
}

function App() {
  return (
    <ErrorBoundary
      FallbackComponent={ErrorFallback}
      onError={(error) => crashlytics().recordError(error)}
    >
      <NavigationContainer>
        <RootNavigator />
      </NavigationContainer>
    </ErrorBoundary>
  );
}

5. Blocking the JS Thread

// BAD: Heavy computation on JS thread
function DataProcessor({ rawData }) {
  // This blocks the JS thread and causes jank
  const processedData = heavyComputation(rawData);
  return <DataVisualization data={processedData} />;
}

// GOOD: Use InteractionManager or move to native
import { InteractionManager } from 'react-native';

function DataProcessor({ rawData }) {
  const [processedData, setProcessedData] = useState(null);

  useEffect(() => {
    // Wait for animations to complete
    InteractionManager.runAfterInteractions(() => {
      const result = heavyComputation(rawData);
      setProcessedData(result);
    });
  }, [rawData]);

  if (!processedData) return <LoadingSpinner />;
  return <DataVisualization data={processedData} />;
}

Refactoring Process

Step 1: Analyze the Component/Screen

  1. Read the entire component and understand its purpose
  2. Identify responsibilities (data fetching, state management, UI rendering, navigation)
  3. List all props, state, effects, and native dependencies
  4. Note any code smells or anti-patterns
  5. Check platform-specific requirements

Step 2: Plan the Refactoring

  1. Determine if the component should be split
  2. Identify logic that can be extracted to custom hooks
  3. Plan platform-specific code separation
  4. Consider navigation and deep linking improvements
  5. Plan TypeScript type improvements

Step 3: Execute Incrementally

  1. Extract custom hooks first (preserves behavior)
  2. Split large screens into smaller components
  3. Apply memoization for lists and expensive computations
  4. Separate platform-specific code into .ios.js/.android.js files
  5. Add/improve TypeScript types
  6. Fix anti-patterns

Step 4: Verify

  1. Test on both iOS and Android
  2. Verify navigation works correctly
  3. Check performance with Flipper/React DevTools
  4. Ensure TypeScript compiles without errors
  5. Test edge cases (loading, error, empty states, offline)

Output Format

When refactoring React Native code, provide:

  1. Analysis Summary: Brief description of issues found
  2. Refactored Code: Complete, working code with improvements
  3. Changes Made: Bulleted list of specific changes
  4. Platform Considerations: Any iOS/Android-specific notes
  5. Performance Impact: Expected performance improvements

Quality Standards

Code Quality Checklist

  • [ ] Components follow Single Responsibility Principle
  • [ ] No prop drilling beyond 2 levels
  • [ ] All hooks follow rules of hooks
  • [ ] Dependency arrays are complete and correct
  • [ ] TypeScript types are explicit and accurate
  • [ ] StyleSheet.create used instead of inline styles
  • [ ] Keys are stable and unique (not array indices)
  • [ ] Error boundaries wrap critical sections
  • [ ] Loading and error states are handled
  • [ ] Platform-specific code properly separated

Performance Checklist

  • [ ] FlatList has memoized renderItem
  • [ ] FlatList has getItemLayout for fixed-height items
  • [ ] Images use caching (expo-image or FastImage)
  • [ ] Animations use Reanimated (UI thread)
  • [ ] Heavy computations use InteractionManager
  • [ ] Bundle uses code splitting and lazy loading
  • [ ] New Architecture enabled for compatible libraries

React Native-Specific Checklist

  • [ ] SafeAreaView used for notched devices
  • [ ] StatusBar configured properly
  • [ ] Keyboard avoiding behavior implemented
  • [ ] Deep linking configured
  • [ ] Push notifications handled
  • [ ] App state changes handled (foreground/background)

When to Stop Refactoring

Stop refactoring when:

  1. Component is under 100 lines and has single responsibility
  2. State is colocated - each piece of state is as close as possible to where it's used
  3. Props are minimal - component receives only what it needs
  4. No obvious code smells remain
  5. TypeScript is happy - no any types, no type errors
  6. Tests pass - behavior is preserved on both platforms
  7. Further changes would be premature optimization - don't optimize without evidence of performance issues
  8. FlatList performs well - smooth scrolling at 60fps

References

平台分布

OpenCode

28.54%
按下载量换算92

Claude Code

21.41%
按下载量换算69

Antigravity

16.44%
按下载量换算53

windsurf

11.47%
按下载量换算37

Codex

7.44%
按下载量换算24

Gemini CLI

2.97%
按下载量换算10

安全审计

暂无安全审计结果可展示。

权限和风险

权限未标注

当前来源未能明确判断权限范围,默认进入异常复核队列。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。目前只有一个公开来源,建议同时核对源仓库。

继续浏览同类 Skills