$npx -y skills add virgo777/buddyme --skill frontend-patterns针对 React、Next.js、状态管理、性能优化及 UI 最佳实践的前端开发模式。
| 1 | # 前端开发模式 (Frontend Development Patterns) |
| 2 | |
| 3 | 适用于 React、Next.js 和高性能用户界面的现代前端模式。 |
| 4 | |
| 5 | ## 何时激活 |
| 6 | |
| 7 | - 构建 React 组件(组合、Props、渲染) |
| 8 | - 管理状态(useState、useReducer、Zustand、Context) |
| 9 | - 实现数据获取(SWR、React Query、服务端组件) |
| 10 | - 优化性能(记忆化、虚拟化、代码分割) |
| 11 | - 处理表单(验证、受控输入、Zod 模式) |
| 12 | - 处理客户端路由与导航 |
| 13 | - 构建可访问(Accessible)、响应式的 UI 模式 |
| 14 | |
| 15 | ## 组件模式 (Component Patterns) |
| 16 | |
| 17 | ### 组合优于继承 (Composition Over Inheritance) |
| 18 | |
| 19 | ```typescript |
| 20 | // ✅ 推荐:组件组合 |
| 21 | interface CardProps { |
| 22 | children: React.ReactNode |
| 23 | variant?: 'default' | 'outlined' |
| 24 | } |
| 25 | |
| 26 | export function Card({ children, variant = 'default' }: CardProps) { |
| 27 | return <div className={`card card-${variant}`}>{children}</div> |
| 28 | } |
| 29 | |
| 30 | export function CardHeader({ children }: { children: React.ReactNode }) { |
| 31 | return <div className="card-header">{children}</div> |
| 32 | } |
| 33 | |
| 34 | export function CardBody({ children }: { children: React.ReactNode }) { |
| 35 | return <div className="card-body">{children}</div> |
| 36 | } |
| 37 | |
| 38 | // 使用示例 |
| 39 | <Card> |
| 40 | <CardHeader>Title</CardHeader> |
| 41 | <CardBody>Content</CardBody> |
| 42 | </Card> |
| 43 | ``` |
| 44 | |
| 45 | ### 复合组件 (Compound Components) |
| 46 | |
| 47 | ```typescript |
| 48 | interface TabsContextValue { |
| 49 | activeTab: string |
| 50 | setActiveTab: (tab: string) => void |
| 51 | } |
| 52 | |
| 53 | const TabsContext = createContext<TabsContextValue | undefined>(undefined) |
| 54 | |
| 55 | export function Tabs({ children, defaultTab }: { |
| 56 | children: React.ReactNode |
| 57 | defaultTab: string |
| 58 | }) { |
| 59 | const [activeTab, setActiveTab] = useState(defaultTab) |
| 60 | |
| 61 | return ( |
| 62 | <TabsContext.Provider value={{ activeTab, setActiveTab }}> |
| 63 | {children} |
| 64 | </TabsContext.Provider> |
| 65 | ) |
| 66 | } |
| 67 | |
| 68 | export function TabList({ children }: { children: React.ReactNode }) { |
| 69 | return <div className="tab-list">{children}</div> |
| 70 | } |
| 71 | |
| 72 | export function Tab({ id, children }: { id: string, children: React.ReactNode }) { |
| 73 | const context = useContext(TabsContext) |
| 74 | if (!context) throw new Error('Tab must be used within Tabs') |
| 75 | |
| 76 | return ( |
| 77 | <button |
| 78 | className={context.activeTab === id ? 'active' : ''} |
| 79 | onClick={() => context.setActiveTab(id)} |
| 80 | > |
| 81 | {children} |
| 82 | </button> |
| 83 | ) |
| 84 | } |
| 85 | |
| 86 | // 使用示例 |
| 87 | <Tabs defaultTab="overview"> |
| 88 | <TabList> |
| 89 | <Tab id="overview">Overview</Tab> |
| 90 | <Tab id="details">Details</Tab> |
| 91 | </TabList> |
| 92 | </Tabs> |
| 93 | ``` |
| 94 | |
| 95 | ### Render Props 模式 (Render Props Pattern) |
| 96 | |
| 97 | ```typescript |
| 98 | interface DataLoaderProps<T> { |
| 99 | url: string |
| 100 | children: (data: T | null, loading: boolean, error: Error | null) => React.ReactNode |
| 101 | } |
| 102 | |
| 103 | export function DataLoader<T>({ url, children }: DataLoaderProps<T>) { |
| 104 | const [data, setData] = useState<T | null>(null) |
| 105 | const [loading, setLoading] = useState(true) |
| 106 | const [error, setError] = useState<Error | null>(null) |
| 107 | |
| 108 | useEffect(() => { |
| 109 | fetch(url) |
| 110 | .then(res => res.json()) |
| 111 | .then(setData) |
| 112 | .catch(setError) |
| 113 | .finally(() => setLoading(false)) |
| 114 | }, [url]) |
| 115 | |
| 116 | return <>{children(data, loading, error)}</> |
| 117 | } |
| 118 | |
| 119 | // 使用示例 |
| 120 | <DataLoader<Market[]> url="/api/markets"> |
| 121 | {(markets, loading, error) => { |
| 122 | if (loading) return <Spinner /> |
| 123 | if (error) return <Error error={error} /> |
| 124 | return <MarketList markets={markets!} /> |
| 125 | }} |
| 126 | </DataLoader> |
| 127 | ``` |
| 128 | |
| 129 | ## 自定义 Hook 模式 (Custom Hooks Patterns) |
| 130 | |
| 131 | ### 状态管理 Hook (State Management Hook) |
| 132 | |
| 133 | ```typescript |
| 134 | export function useToggle(initialValue = false): [boolean, () => void] { |
| 135 | const [value, setValue] = useState(initialValue) |
| 136 | |
| 137 | const toggle = useCallback(() => { |
| 138 | setValue(v => !v) |
| 139 | }, []) |
| 140 | |
| 141 | return [value, toggle] |
| 142 | } |
| 143 | |
| 144 | // 使用示例 |
| 145 | const [isOpen, toggleOpen] = useToggle() |
| 146 | ``` |
| 147 | |
| 148 | ### 异步数据获取 Hook (Async Data Fetching Hook) |
| 149 | |
| 150 | ```typescript |
| 151 | interface UseQueryOptions<T> { |
| 152 | onSuccess?: (data: T) => void |
| 153 | onError?: (error: Error) => void |
| 154 | enabled?: boolean |
| 155 | } |
| 156 | |
| 157 | export function useQuery<T>( |
| 158 | key: string, |
| 159 | fetcher: () => Promise<T>, |
| 160 | options?: UseQueryOptions<T> |
| 161 | ) { |
| 162 | const [data, setData] = useState<T | null>(null) |
| 163 | const [error, setError] = useState<Error | null>(null) |
| 164 | const [loading, setLoading] = useState(false) |
| 165 | |
| 166 | const refetch = useCallback(async () => { |
| 167 | setLoading(true) |
| 168 | setError(null) |
| 169 | |
| 170 | try { |
| 171 | const result = await fetcher() |
| 172 | setData(result) |
| 173 | options?.onSuccess?.(result) |
| 174 | } catch (err) { |
| 175 | const error = err as Error |
| 176 | setError(error) |
| 177 | options?.onError?.(error) |
| 178 | } finally { |
| 179 | setLoading(false) |
| 180 | } |
| 181 | }, [fetcher, options]) |
| 182 | |
| 183 | useEffect(() => { |
| 184 | if (options?.enabled !== false) { |
| 185 | refetch() |
| 186 | } |
| 187 | }, [key, refetch, options?.enabled]) |
| 188 | |
| 189 | return { data, error, loading, refetch } |
| 190 | } |
| 191 | |
| 192 | // 使用示例 |
| 193 | const { data: markets, loading, error, refetch } = useQuery( |
| 194 | 'markets', |
| 195 | () => fetch('/api/markets').then(r => r.json()), |
| 196 | { |
| 197 | onSuccess: data => console.log('Fetched', data.length, 'markets'), |
| 198 | onError: err => console.error('Failed:', err) |
| 199 | } |
| 200 | ) |
| 201 | ``` |
| 202 | |
| 203 | ### 防抖 Hook (Debounce Hook) |
| 204 | |
| 205 | ```typescript |
| 206 | export function useDebounce<T>(value: T, delay: number): T { |
| 207 | const [debouncedValue, setDebouncedValue] = useState<T>(value) |
| 208 | |
| 209 | useEffect(() => { |
| 210 | const handler = setTimeout(() => { |
| 211 | setDebouncedValue(value) |
| 212 | }, delay) |
| 213 | |
| 214 | return () => clearTimeout(handler) |
| 215 | }, [ |