bulletproof-react-patterns
8
总安装量
8
周安装量
#35606
全站排名
安装命令
npx skills add https://github.com/grahamcrackers/skills --skill bulletproof-react-patterns
Agent 安装分布
gemini-cli
8
github-copilot
8
codex
8
kimi-cli
8
cursor
8
opencode
8
Skill 文档
Bulletproof React Patterns
Architecture patterns for building scalable, maintainable React applications. Based on bulletproof-react.
Core References
| Topic | Description | Reference |
|---|---|---|
| Project Structure | Feature-based organization, unidirectional architecture, ESLint enforcement | project-structure |
| Components & Styling | Component hierarchy, wrapping 3rd party libs, headless vs styled libraries | components-and-styling |
| API Layer | API client, request declarations, query/mutation hook patterns | api-layer |
| State Management | Component, application, server cache, form, and URL state categories | state-management |
| Error Handling | Error boundaries, API errors, error tracking with Sentry | error-handling |
| Testing | Unit, integration, e2e strategies with Vitest, Testing Library, Playwright, MSW | testing |
| Project Standards | ESLint, Prettier, TypeScript, Husky, absolute imports, file naming | project-standards |
| Security | Authentication, token storage, XSS prevention, RBAC/PBAC authorization | security |
| Performance | Code splitting, data prefetching, state optimization, children pattern | performance |
Project Structure
Organize by feature, not by file type:
src/
âââ app/ # Application shell (routes, providers, router)
âââ assets/ # Static files (images, fonts)
âââ components/ # Shared, reusable UI components
âââ config/ # Environment variables, constants
âââ features/ # Feature-based modules
âââ hooks/ # Shared custom hooks
âââ lib/ # Pre-configured library wrappers
âââ stores/ # Global client state
âââ testing/ # Test utilities, MSW handlers, factories
âââ types/ # Shared TypeScript types
âââ utils/ # Pure utility functions
Feature Modules
features/users/
âââ api/ # API functions and query hooks
âââ components/ # Feature-specific components
âââ hooks/ # Feature-specific hooks
âââ types/ # Feature-specific types
âââ utils/ # Feature-specific utilities
Rules:
- Features should not import from other features. Compose at the app level.
- Code flows one direction: shared â features â app.
- Promote to shared directories only when reused by 2+ features.
- Prefer direct imports over barrel re-exports for Vite tree-shaking.
Component Hierarchy
Page Components â route-level, compose features, handle layout
âââ Feature Components â feature-specific, business logic
âââ UI Components â shared primitives, no business logic
API Layer Pattern
// Pure API function
function getUsers(params?: GetUsersParams): Promise<UsersResponse> {
return api.get("/users", { params });
}
// Query hook wrapping the API function
function useUsers(params?: GetUsersParams) {
return useQuery({
queryKey: ["users", params],
queryFn: () => getUsers(params),
});
}
State Management Boundaries
| State Type | Solution | Examples |
|---|---|---|
| Server state | TanStack Query | User data, posts, API responses |
| Client state (global) | Zustand / Jotai | Theme, sidebar open, user preferences |
| Client state (local) | useState / useReducer | Form inputs, toggles, modal open |
| URL state | URL search params / router | Filters, pagination, active tab |
| Form state | React Hook Form | Multi-step forms, validation |
Don’t mix server and client state. Never copy query data into useState.
Error Hierarchy
App Error Boundary â catches unrecoverable crashes
âââ Route Error Boundary â catches route-level failures, shows retry
âââ Feature Error Boundary â catches feature-specific errors
Testing Strategy
| Layer | Tool | What to Test |
|---|---|---|
| Components | Testing Library | Render output, user interactions, a11y |
| Hooks | renderHook | State changes, side effects |
| API | MSW | Request/response handling, error states |
| Integration | Testing Library + MSW | Full feature flows (render â interact â verify) |
| E2E | Playwright | Critical user journeys |
Conventions
| Item | Convention | Example |
|---|---|---|
| Components | PascalCase | UserCard.tsx |
| Hooks | camelCase, use prefix |
useUsers.ts |
| Utilities | camelCase | formatDate.ts |
| Types | PascalCase | User, CreateUserInput |
| Constants | UPPER_SNAKE_CASE | MAX_RETRIES |
| Directories | kebab-case | user-settings/ |
| Files | kebab-case | user-card.tsx |
Imports
Use path aliases to avoid deep relative imports:
import { Button } from "@/components/ui/button";
import { useUsers } from "@/features/users/api";
Configure @/ as the src/ alias in tsconfig.json.