1---
2name: react
3description: This skill should be used when the user asks about "React", "React components", "React hooks", "useState", "useEffect", "JSX", "React patterns", "React best practices", or mentions React development.
4version: 1.0.0
5metadata:
6 source: https://skillmd.com/skills/eyadsibai/react
7---
8
9# React Development
10
11Guidance for building React applications with modern patterns.
12
13---
14
15## Hooks Reference
16
17### Built-in Hooks
18
19| Hook | Purpose | When to Use |
20|------|---------|-------------|
21| **useState** | Local state | Simple values, toggles, form inputs |
22| **useReducer** | Complex state | Multiple sub-values, state machines |
23| **useEffect** | Side effects | Data fetching, subscriptions, DOM manipulation |
24| **useContext** | Consume context | Theme, auth, global state |
25| **useRef** | Mutable reference | DOM access, persist values across renders |
26| **useMemo** | Memoize value | Expensive calculations |
27| **useCallback** | Memoize function | Stable callbacks for child components |
28| **useLayoutEffect** | Sync DOM effects | Measure DOM before paint |
29| **useId** | Unique IDs | Accessibility, form labels |
30
31### Hook Rules
32
33| Rule | Why |
34|------|-----|
35| Only call at top level | Hooks rely on call order |
36| Only call from React functions | Components or custom hooks |
37| Name custom hooks with "use" | Convention for linting |
38
39---
40
41## Component Patterns
42
43| Pattern | Use Case | Key Concept |
44|---------|----------|-------------|
45| **Presentational** | Pure UI, no logic | Props in, JSX out |
46| **Container** | Data fetching, state | Wraps presentational |
47| **Compound Components** | Related components sharing state | Parent provides context |
48| **Render Props** | Share behavior | Function as child |
49| **Custom Hooks** | Reusable stateful logic | Extract from components |
50| **HOC** | Legacy pattern | Prefer hooks instead |
51
52---
53
54## State Management Decision
55
56| Solution | Best For | Trade-offs |
57|----------|----------|------------|
58| **useState** | Component-local | Simplest, prop drilling for sharing |
59| **useReducer** | Complex local state | More boilerplate, predictable |
60| **Context** | Theme, auth, i18n | Re-renders all consumers |
61| **Zustand** | Simple global | Minimal API, good devtools |
62| **Redux Toolkit** | Complex global | Powerful, more boilerplate |
63| **React Query/TanStack** | Server state | Caching, background refresh |
64| **Jotai/Recoil** | Atomic state | Fine-grained updates |
65
66**Key concept**: Server state (API data) and client state (UI state) should be managed differently. Use React Query for server state.
67
68---
69
70## useEffect Patterns
71
72| Pattern | Dependency Array | Purpose |
73|---------|------------------|---------|
74| Run once | `[]` | Initial fetch, setup |
75| Run on change | `[dep1, dep2]` | React to specific changes |
76| Run every render | (omit) | Rarely needed |
77| Cleanup | Return function | Subscriptions, timers |
78
79### Common Pitfalls
80
81| Pitfall | Solution |
82|---------|----------|
83| Infinite loops | Include all dependencies, use useCallback |
84| Stale closures | Use refs or functional updates |
85| Race conditions | Cleanup flag or AbortController |
86| Missing deps | Trust the linter, refactor if needed |
87
88---
89
90## Performance Optimization
91
92| Technique | When to Use | Cost |
93|-----------|-------------|------|
94| **React.memo** | Expensive child, frequent parent renders | Shallow comparison |
95| **useMemo** | Expensive calculation | Memory for cached value |
96| **useCallback** | Callback passed to memoized child | Memory for function |
97| **Lazy loading** | Large components, routes | Initial load vs waterfall |
98| **Virtualization** | Long lists (100+ items) | Complexity |
99
100**Key concept**: Don't optimize prematurely. Measure first with React DevTools Profiler.
101
102---
103
104## Conditional Rendering
105
106| Pattern | Use Case |
107|---------|----------|
108| **&&** | Show/hide single element |
109| **Ternary** | Two alternatives |
110| **Early return** | Multiple conditions, cleaner |
111| **Switch/object map** | Many alternatives |
112
113---
114
115## Form Handling
116
117| Approach | Use Case |
118|----------|----------|
119| **Controlled** | Need validation, formatting |
120| **Uncontrolled (refs)** | Simple forms, performance |
121| **React Hook Form** | Complex forms, validation |
122| **Formik** | Legacy, full-featured |
123
124**Key concept**: Controlled components store value in React state. Uncontrolled use DOM as source of truth.
125
126---
127
128## Project Structure
129
130| Directory | Purpose |
131|-----------|---------|
132| **components/ui/** | Generic, reusable (Button, Card) |
133| **components/features/** | Feature-specific compositions |
134| **hooks/** | Custom hooks |
135| **contexts/** | React contexts + providers |
136| **pages/** or **routes/** | Route components |
137| **services/** or **api/** | API calls, data fetching |
138| **types/** | TypeScript types |
139| **utils/** | Pure helper functions |
140
141---
142
143## Best Practices
144
145| Practice | Why |
146|----------|-----|
147| Keep components small (<100 lines) | Readable, testable |
148| Colocate related code | Easier to maintain |
149| Lift state only when needed | Avoid unnecessary complexity |
150| Use TypeScript for props | Catch errors, better DX |
151| Prefer composition over inheritance | React's design philosophy |
152| Extract custom hooks | Reusable logic, testable |
153
154## Resources
155
156- React Docs: <https://react.dev/>
157- Patterns: <https://patterns.dev/>