Refactor high-complexity React components in Dify frontend. Use when `pnpm analyze-component...
457
37
# Install this skill:
npx skills add alinaqi/claude-bootstrap --skill "react-native"
Install specific skill from multi-skill repository
# Description
React Native mobile patterns, platform-specific code
# SKILL.md
name: react-native
description: React Native mobile patterns, platform-specific code
React Native Skill
Load with: base.md + typescript.md
Project Structure
project/
βββ src/
β βββ core/ # Pure business logic (no React)
β β βββ types.ts
β β βββ services/
β βββ components/ # Reusable UI components
β β βββ Button/
β β β βββ Button.tsx
β β β βββ Button.test.tsx
β β β βββ index.ts
β β βββ index.ts # Barrel export
β βββ screens/ # Screen components
β β βββ Home/
β β β βββ HomeScreen.tsx
β β β βββ useHome.ts # Screen-specific hook
β β β βββ index.ts
β β βββ index.ts
β βββ navigation/ # Navigation configuration
β βββ hooks/ # Shared custom hooks
β βββ store/ # State management
β βββ utils/ # Utilities
βββ __tests__/
βββ android/
βββ ios/
βββ CLAUDE.md
Component Patterns
Functional Components Only
// Good - simple, testable
interface ButtonProps {
label: string;
onPress: () => void;
disabled?: boolean;
}
export function Button({ label, onPress, disabled = false }: ButtonProps): JSX.Element {
return (
<Pressable onPress={onPress} disabled={disabled}>
<Text>{label}</Text>
</Pressable>
);
}
Extract Logic to Hooks
// useHome.ts - all logic here
export function useHome() {
const [items, setItems] = useState<Item[]>([]);
const [loading, setLoading] = useState(false);
const refresh = useCallback(async () => {
setLoading(true);
const data = await fetchItems();
setItems(data);
setLoading(false);
}, []);
return { items, loading, refresh };
}
// HomeScreen.tsx - pure presentation
export function HomeScreen(): JSX.Element {
const { items, loading, refresh } = useHome();
return (
<ItemList items={items} loading={loading} onRefresh={refresh} />
);
}
Props Interface Always Explicit
// Always define props interface, even if simple
interface ItemCardProps {
item: Item;
onPress: (id: string) => void;
}
export function ItemCard({ item, onPress }: ItemCardProps): JSX.Element {
...
}
State Management
Local State First
// Start with useState, escalate only when needed
const [value, setValue] = useState('');
Zustand for Global State (if needed)
// store/useAppStore.ts
import { create } from 'zustand';
interface AppState {
user: User | null;
setUser: (user: User | null) => void;
}
export const useAppStore = create<AppState>((set) => ({
user: null,
setUser: (user) => set({ user }),
}));
React Query for Server State
// hooks/useItems.ts
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
export function useItems() {
return useQuery({
queryKey: ['items'],
queryFn: fetchItems,
});
}
export function useCreateItem() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: createItem,
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['items'] });
},
});
}
Testing
Component Testing with React Native Testing Library
import { render, fireEvent } from '@testing-library/react-native';
import { Button } from './Button';
describe('Button', () => {
it('calls onPress when pressed', () => {
const onPress = jest.fn();
const { getByText } = render(<Button label="Click me" onPress={onPress} />);
fireEvent.press(getByText('Click me'));
expect(onPress).toHaveBeenCalledTimes(1);
});
it('does not call onPress when disabled', () => {
const onPress = jest.fn();
const { getByText } = render(<Button label="Click me" onPress={onPress} disabled />);
fireEvent.press(getByText('Click me'));
expect(onPress).not.toHaveBeenCalled();
});
});
Hook Testing
import { renderHook, act } from '@testing-library/react-hooks';
import { useCounter } from './useCounter';
describe('useCounter', () => {
it('increments counter', () => {
const { result } = renderHook(() => useCounter());
act(() => {
result.current.increment();
});
expect(result.current.count).toBe(1);
});
});
Platform-Specific Code
Use Platform.select Sparingly
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: 2,
},
}),
});
Separate Files for Complex Differences
Component/
βββ Component.tsx # Shared logic
βββ Component.ios.tsx # iOS-specific
βββ Component.android.tsx # Android-specific
βββ index.ts
React Native Anti-Patterns
- β Inline styles - use StyleSheet.create
- β Logic in render - extract to hooks
- β Deep component nesting - flatten hierarchy
- β Anonymous functions in props - use useCallback
- β Index as key in lists - use stable IDs
- β Direct state mutation - always use setter
- β Mixing business logic with UI - keep core/ pure
- β Ignoring TypeScript errors - fix them
- β Large components - split into smaller pieces
# Supported AI Coding Agents
This skill is compatible with the SKILL.md standard and works with all major AI coding agents:
Amp
Antigravity
Claude Code
Clawdbot
Codex
Cursor
Droid
Gemini CLI
GitHub Copilot
Goose
Kilo Code
Kiro CLI
OpenCode
Roo Code
Trae
Windsurf
Learn more about the SKILL.md standard and how to use these skills with your preferred AI coding agent.