$npx -y skills add sabahattink/antigravity-fullstack-hq --skill react-best-practicesReact component patterns, hooks, state management, and performance best practices. Use when building or reviewing React components.
| 1 | # React Best Practices |
| 2 | |
| 3 | ## Component Patterns |
| 4 | |
| 5 | ### Functional Components Only |
| 6 | |
| 7 | ```typescript |
| 8 | // ✅ Correct |
| 9 | interface UserCardProps { |
| 10 | user: User |
| 11 | onSelect: (id: string) => void |
| 12 | } |
| 13 | |
| 14 | export const UserCard = ({ user, onSelect }: UserCardProps) => { |
| 15 | return ( |
| 16 | <button onClick={() => onSelect(user.id)} className="..."> |
| 17 | {user.name} |
| 18 | </button> |
| 19 | ) |
| 20 | } |
| 21 | |
| 22 | // ❌ Never use class components |
| 23 | class UserCard extends React.Component {} |
| 24 | ``` |
| 25 | |
| 26 | ### Composition Over Props Drilling |
| 27 | |
| 28 | ```typescript |
| 29 | // ✅ Use composition |
| 30 | export const Layout = ({ children }: { children: React.ReactNode }) => ( |
| 31 | <div className="layout">{children}</div> |
| 32 | ) |
| 33 | |
| 34 | // ✅ Use context for deep data |
| 35 | const ThemeContext = createContext<Theme | null>(null) |
| 36 | export const useTheme = () => { |
| 37 | const theme = useContext(ThemeContext) |
| 38 | if (!theme) throw new Error('useTheme must be used within ThemeProvider') |
| 39 | return theme |
| 40 | } |
| 41 | ``` |
| 42 | |
| 43 | ## Hooks |
| 44 | |
| 45 | ### Custom Hooks |
| 46 | |
| 47 | ```typescript |
| 48 | // ✅ Extract reusable logic into hooks |
| 49 | const useLocalStorage = <T>(key: string, initial: T) => { |
| 50 | const [value, setValue] = useState<T>(() => { |
| 51 | try { |
| 52 | const item = localStorage.getItem(key) |
| 53 | return item ? JSON.parse(item) : initial |
| 54 | } catch { |
| 55 | return initial |
| 56 | } |
| 57 | }) |
| 58 | |
| 59 | const set = (val: T) => { |
| 60 | setValue(val) |
| 61 | localStorage.setItem(key, JSON.stringify(val)) |
| 62 | } |
| 63 | |
| 64 | return [value, set] as const |
| 65 | } |
| 66 | ``` |
| 67 | |
| 68 | ### useEffect Rules |
| 69 | |
| 70 | ```typescript |
| 71 | // ✅ Correct — explicit dependencies |
| 72 | useEffect(() => { |
| 73 | fetchUser(userId) |
| 74 | }, [userId]) |
| 75 | |
| 76 | // ✅ Cleanup when needed |
| 77 | useEffect(() => { |
| 78 | const sub = subscribe(channel) |
| 79 | return () => sub.unsubscribe() |
| 80 | }, [channel]) |
| 81 | |
| 82 | // ❌ Never ignore the dependency array |
| 83 | useEffect(() => { |
| 84 | fetchUser(userId) |
| 85 | }) // runs on every render |
| 86 | ``` |
| 87 | |
| 88 | ## Performance |
| 89 | |
| 90 | ### Memoization |
| 91 | |
| 92 | ```typescript |
| 93 | // ✅ Memoize expensive computations |
| 94 | const sorted = useMemo( |
| 95 | () => items.sort((a, b) => a.name.localeCompare(b.name)), |
| 96 | [items] |
| 97 | ) |
| 98 | |
| 99 | // ✅ Stable callback references |
| 100 | const handleClick = useCallback((id: string) => { |
| 101 | onSelect(id) |
| 102 | }, [onSelect]) |
| 103 | |
| 104 | // ✅ Prevent unnecessary re-renders |
| 105 | export const HeavyList = memo(({ items }: { items: Item[] }) => ( |
| 106 | <ul>{items.map(item => <li key={item.id}>{item.name}</li>)}</ul> |
| 107 | )) |
| 108 | ``` |
| 109 | |
| 110 | ### Code Splitting |
| 111 | |
| 112 | ```typescript |
| 113 | // ✅ Lazy load heavy components |
| 114 | const Dashboard = lazy(() => import('./Dashboard')) |
| 115 | |
| 116 | export const App = () => ( |
| 117 | <Suspense fallback={<Spinner />}> |
| 118 | <Dashboard /> |
| 119 | </Suspense> |
| 120 | ) |
| 121 | ``` |
| 122 | |
| 123 | ## Error Handling |
| 124 | |
| 125 | ```typescript |
| 126 | // ✅ Error boundaries for UI errors |
| 127 | export class ErrorBoundary extends React.Component< |
| 128 | { children: ReactNode; fallback: ReactNode }, |
| 129 | { hasError: boolean } |
| 130 | > { |
| 131 | state = { hasError: false } |
| 132 | static getDerivedStateFromError() { return { hasError: true } } |
| 133 | render() { |
| 134 | return this.state.hasError ? this.props.fallback : this.props.children |
| 135 | } |
| 136 | } |
| 137 | ``` |
| 138 | |
| 139 | ## Forbidden Patterns |
| 140 | |
| 141 | - Class components (use functional) |
| 142 | - `any` types on props |
| 143 | - Mutating state directly |
| 144 | - Missing `key` props in lists |
| 145 | - Ignoring cleanup in `useEffect` |
| 146 | - `useEffect` for derived state (use `useMemo`) |