React UI Patterns
Core Principles
- •Never show stale UI - Loading spinners only when actually loading.
- •Always surface errors - Users must know when something fails.
- •Optimistic updates - Make the UI feel instant.
- •Performance First - Don't block the main thread.
- •Graceful degradation - Partial data is better than no data.
Loading State Patterns
[!TIP] The Golden Rule: Show loading indicator ONLY when there's no data to display.
Decision Tree
- •Error? → Show Error State (with Retry).
- •Loading AND No Data? → Show Loading Indicator.
- •Data Exists? → Show Content (even if refreshing).
Error Handling Patterns
[!IMPORTANT] CRITICAL: Never swallow errors silently.
Strategy
- •Field Level: Validation errors inline.
- •Toast: Transient/Recoverable errors.
- •Banner: Persistent but partial functionality.
- •Full Screen: Catastrophic failure.
Performance Optimization
[!NOTE] Optimization adds complexity. Measure first.
Quick Wins
- •Use
React.memofor pure components (lists, UI kits). - •VIRTUALIZE long lists (
react-window). - •Use
useCallbackfor functions passed to memoized children. - •Use
useTransitionfor non-urgent state updates (avoid UI freezing).
Button State Patterns
CRITICAL: Always disable triggers during async operations.
tsx
// CORRECT - Button disabled while loading
<Button
disabled={isSubmitting} // Prevent double-tap
isLoading={isSubmitting} // Feedback
onClick={handleSubmit}
>
Submit
</Button>
Empty States
Every list/collection MUST have an empty state.
tsx
// CORRECT - Explicit empty state
return (
<FlatList
data={items}
ListEmptyComponent={
<EmptyState
title="No items"
description="Create one to get started"
action={<CreateButton />}
/>
}
/>
);
Checklist
Before completing any UI component:
UI States:
- • Error state handled and shown to user?
- • Loading state shown ONLY when no data exists (no flash)?
- • Empty state provided for collections?
- • Buttons disabled during async operations?
Performance:
- • Are list keys stable?
- • Are heavy lists virtualized?
- • Is
console.logremoved?
Integration with Other Skills
- •graphql-schema: Use mutation patterns with proper error handling.
- •testing-patterns: Test all UI states (loading, error, empty, success).
- •formik-patterns: Apply form submission patterns.