[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill-ab6592f4-aded-4513-9a75-00c837094583":3,"$f_PRrCgxb5MAg-PXBmXZPG7Qy4J73jzS07-EfXS5KGVs":43},{"id":4,"title":5,"description":6,"categoryId":7,"moduleId":8,"tags":9,"prompt":10,"icon":11,"source":12,"sourceUrl":13,"authorId":14,"authorName":15,"isPublic":16,"stars":17,"runs":18,"createdAt":19,"updatedAt":19,"module":20,"category":27,"packages":34},"ab6592f4-aded-4513-9a75-00c837094583","react-ui-patterns","现代React UI加载状态、错误处理和数据获取模式。用于构建UI组件、处理异步数据或管理UI状态时使用。","cat_coding_frontend","mod_coding","sickn33,coding","---\nname: react-ui-patterns\ndescription: \"Modern React UI patterns for loading states, error handling, and data fetching. Use when building UI components, handling async data, or managing UI states.\"\nrisk: unknown\nsource: community\ndate_added: \"2026-02-27\"\n---\n\n# React UI Patterns\n\n## Core Principles\n\n1. **Never show stale UI** - Loading spinners only when actually loading\n2. **Always surface errors** - Users must know when something fails\n3. **Optimistic updates** - Make the UI feel instant\n4. **Progressive disclosure** - Show content as it becomes available\n5. **Graceful degradation** - Partial data is better than no data\n\n## Loading State Patterns\n\n### The Golden Rule\n\n**Show loading indicator ONLY when there's no data to display.**\n\n```typescript\n\u002F\u002F CORRECT - Only show loading when no data exists\nconst { data, loading, error } = useGetItemsQuery();\n\nif (error) return \u003CErrorState error={error} onRetry={refetch} \u002F>;\nif (loading && !data) return \u003CLoadingState \u002F>;\nif (!data?.items.length) return \u003CEmptyState \u002F>;\n\nreturn \u003CItemList items={data.items} \u002F>;\n```\n\n```typescript\n\u002F\u002F WRONG - Shows spinner even when we have cached data\nif (loading) return \u003CLoadingState \u002F>; \u002F\u002F Flashes on refetch!\n```\n\n### Loading State Decision Tree\n\n```\nIs there an error?\n  → Yes: Show error state with retry option\n  → No: Continue\n\nIs it loading AND we have no data?\n  → Yes: Show loading indicator (spinner\u002Fskeleton)\n  → No: Continue\n\nDo we have data?\n  → Yes, with items: Show the data\n  → Yes, but empty: Show empty state\n  → No: Show loading (fallback)\n```\n\n### Skeleton vs Spinner\n\n| Use Skeleton When | Use Spinner When |\n|-------------------|------------------|\n| Known content shape | Unknown content shape |\n| List\u002Fcard layouts | Modal actions |\n| Initial page load | Button submissions |\n| Content placeholders | Inline operations |\n\n## Error Handling Patterns\n\n### The Error Handling Hierarchy\n\n```\n1. Inline error (field-level) → Form validation errors\n2. Toast notification → Recoverable errors, user can retry\n3. Error banner → Page-level errors, data still partially usable\n4. Full error screen → Unrecoverable, needs user action\n```\n\n### Always Show Errors\n\n**CRITICAL: Never swallow errors silently.**\n\n```typescript\n\u002F\u002F CORRECT - Error always surfaced to user\nconst [createItem, { loading }] = useCreateItemMutation({\n  onCompleted: () => {\n    toast.success({ title: 'Item created' });\n  },\n  onError: (error) => {\n    console.error('createItem failed:', error);\n    toast.error({ title: 'Failed to create item' });\n  },\n});\n\n\u002F\u002F WRONG - Error silently caught, user has no idea\nconst [createItem] = useCreateItemMutation({\n  onError: (error) => {\n    console.error(error); \u002F\u002F User sees nothing!\n  },\n});\n```\n\n### Error State Component Pattern\n\n```typescript\ninterface ErrorStateProps {\n  error: Error;\n  onRetry?: () => void;\n  title?: string;\n}\n\nconst ErrorState = ({ error, onRetry, title }: ErrorStateProps) => (\n  \u003Cdiv className=\"error-state\">\n    \u003CIcon name=\"exclamation-circle\" \u002F>\n    \u003Ch3>{title ?? 'Something went wrong'}\u003C\u002Fh3>\n    \u003Cp>{error.message}\u003C\u002Fp>\n    {onRetry && (\n      \u003CButton onClick={onRetry}>Try Again\u003C\u002FButton>\n    )}\n  \u003C\u002Fdiv>\n);\n```\n\n## Button State Patterns\n\n### Button Loading State\n\n```tsx\n\u003CButton\n  onClick={handleSubmit}\n  isLoading={isSubmitting}\n  disabled={!isValid || isSubmitting}\n>\n  Submit\n\u003C\u002FButton>\n```\n\n### Disable During Operations\n\n**CRITICAL: Always disable triggers during async operations.**\n\n```tsx\n\u002F\u002F CORRECT - Button disabled while loading\n\u003CButton\n  disabled={isSubmitting}\n  isLoading={isSubmitting}\n  onClick={handleSubmit}\n>\n  Submit\n\u003C\u002FButton>\n\n\u002F\u002F WRONG - User can tap multiple times\n\u003CButton onClick={handleSubmit}>\n  {isSubmitting ? 'Submitting...' : 'Submit'}\n\u003C\u002FButton>\n```\n\n## Empty States\n\n### Empty State Requirements\n\nEvery list\u002Fcollection MUST have an empty state:\n\n```tsx\n\u002F\u002F WRONG - No empty state\nreturn \u003CFlatList data={items} \u002F>;\n\n\u002F\u002F CORRECT - Explicit empty state\nreturn (\n  \u003CFlatList\n    data={items}\n    ListEmptyComponent={\u003CEmptyState \u002F>}\n  \u002F>\n);\n```\n\n### Contextual Empty States\n\n```tsx\n\u002F\u002F Search with no results\n\u003CEmptyState\n  icon=\"search\"\n  title=\"No results found\"\n  description=\"Try different search terms\"\n\u002F>\n\n\u002F\u002F List with no items yet\n\u003CEmptyState\n  icon=\"plus-circle\"\n  title=\"No items yet\"\n  description=\"Create your first item\"\n  action={{ label: 'Create Item', onClick: handleCreate }}\n\u002F>\n```\n\n## Form Submission Pattern\n\n```tsx\nconst MyForm = () => {\n  const [submit, { loading }] = useSubmitMutation({\n    onCompleted: handleSuccess,\n    onError: handleError,\n  });\n\n  const handleSubmit = async () => {\n    if (!isValid) {\n      toast.error({ title: 'Please fix errors' });\n      return;\n    }\n    await submit({ variables: { input: values } });\n  };\n\n  return (\n    \u003Cform>\n      \u003CInput\n        value={values.name}\n        onChange={handleChange('name')}\n        error={touched.name ? errors.name : undefined}\n      \u002F>\n      \u003CButton\n        type=\"submit\"\n        onClick={handleSubmit}\n        disabled={!isValid || loading}\n        isLoading={loading}\n      >\n        Submit\n      \u003C\u002FButton>\n    \u003C\u002Fform>\n  );\n};\n```\n\n## Anti-Patterns\n\n### Loading States\n\n```typescript\n\u002F\u002F WRONG - Spinner when data exists (causes flash)\nif (loading) return \u003CSpinner \u002F>;\n\n\u002F\u002F CORRECT - Only show loading without data\nif (loading && !data) return \u003CSpinner \u002F>;\n```\n\n### Error Handling\n\n```typescript\n\u002F\u002F WRONG - Error swallowed\ntry {\n  await mutation();\n} catch (e) {\n  console.log(e); \u002F\u002F User has no idea!\n}\n\n\u002F\u002F CORRECT - Error surfaced\nonError: (error) => {\n  console.error('operation failed:', error);\n  toast.error({ title: 'Operation failed' });\n}\n```\n\n### Button States\n\n```typescript\n\u002F\u002F WRONG - Button not disabled during submission\n\u003CButton onClick={submit}>Submit\u003C\u002FButton>\n\n\u002F\u002F CORRECT - Disabled and shows loading\n\u003CButton onClick={submit} disabled={loading} isLoading={loading}>\n  Submit\n\u003C\u002FButton>\n```\n\n## Checklist\n\nBefore completing any UI component:\n\n**UI States:**\n- [ ] Error state handled and shown to user\n- [ ] Loading state shown only when no data exists\n- [ ] Empty state provided for collections\n- [ ] Buttons disabled during async operations\n- [ ] Buttons show loading indicator when appropriate\n\n**Data & Mutations:**\n- [ ] Mutations have onError handler\n- [ ] All user actions have feedback (toast\u002Fvisual)\n\n## Integration with Other Skills\n\n- **graphql-schema**: Use mutation patterns with proper error handling\n- **testing-patterns**: Test all UI states (loading, error, empty, success)\n- **formik-patterns**: Apply form submission patterns\n\n## When to Use\nThis skill is applicable to execute the workflow or actions described in the overview.\n\n## Limitations\n- Use this skill only when the task clearly matches the scope described above.\n- Do not treat the output as a substitute for environment-specific validation, testing, or expert review.\n- Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.\n","","imported","https:\u002F\u002Fgithub.com\u002Fsickn33\u002Fantigravity-awesome-skills","user_system_seed","SkillOPIC",true,244,333,"2026-05-16 13:36:37",{"id":8,"name":21,"slug":22,"icon":23,"description":24,"sort":25,"createdAt":26},"编程开发","coding","mdi-code-braces","代码生成、调试、审查，提升开发效率",2,"2026-05-16 12:53:40",{"id":7,"name":28,"slug":29,"icon":30,"description":31,"moduleId":8,"sort":32,"skillCount":33,"createdAt":26},"前端开发","frontend","mdi-language-html5","HTML\u002FCSS\u002FJavaScript\u002F框架相关",1,96,[35],{"id":36,"skillId":4,"version":37,"fileName":38,"fileSize":39,"filePath":40,"fileHash":41,"manifest":42,"createdAt":19},"a38a48ef-bedb-4337-85f0-2c13ed710351","1.0.0","react-ui-patterns.zip",2893,"uploads\u002Fskills\u002Fab6592f4-aded-4513-9a75-00c837094583\u002Freact-ui-patterns.zip","dfb904096f7b641c5ade5fcf52588308d9d98a59d8b5d2402853d15afa06454d","[{\"path\":\"SKILL.md\",\"isDirectory\":false,\"size\":6969}]",{"code":44,"message":45,"data":46},200,"success",{"items":47,"stats":48,"page":51},[],{"averageRating":49,"totalRatings":49,"ratingCounts":50},0,[49,49,49,49,49],{"limit":52,"offset":49,"hasMore":53,"nextOffset":52,"ratedOnly":16},15,false]