|
| 1 | +# React Data Fetch + useMemo (Vite + TypeScript) |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +A small React SPA that fetches product data from a public API and demonstrates **memoization of derived data** using `useMemo`. |
| 9 | + |
| 10 | +This project focuses on **render behavior**, **performance awareness**, and **clean separation between data fetching, UI state, and derived state**. |
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | +## Features |
| 15 | + |
| 16 | +- 🌐 **Data fetching** from a public REST API (`dummyjson.com`) |
| 17 | +- 🔎 **Client-side filtering** by: |
| 18 | + - Search term |
| 19 | + - Category |
| 20 | + - Sort option (title / price / rating) |
| 21 | +- 🧠 **Memoized derived data** using `useMemo` |
| 22 | + - Prevents unnecessary recomputation on theme changes |
| 23 | +- 🧩 Clear separation of concerns: |
| 24 | + - Data fetching (`useProducts`) |
| 25 | + - UI state (filters) |
| 26 | + - Derived state (filtered & sorted products) |
| 27 | +- 🌗 **Light / Dark theme toggle** |
| 28 | + - Implemented as unrelated UI state |
| 29 | + - Demonstrates that memoized derived data is NOT recomputed |
| 30 | +- 🎨 Clean, responsive layout using plain CSS Grid |
| 31 | +- ⚛️ React **StrictMode-friendly** (expected double renders in dev) |
| 32 | + |
| 33 | +--- |
| 34 | + |
| 35 | +## Why this project exists |
| 36 | + |
| 37 | +In React applications, it’s common to derive data from a base dataset |
| 38 | +(e.g. filtering, sorting, mapping). |
| 39 | + |
| 40 | +This project demonstrates: |
| 41 | + |
| 42 | +- **Why derived data should be memoized** |
| 43 | +- **When `useMemo` is useful** |
| 44 | +- How to avoid unnecessary recomputation when **unrelated UI state changes** |
| 45 | +- How unrelated UI state (such as a theme toggle) does not trigger expensive |
| 46 | + recomputations when derived data is properly memoized. |
| 47 | +- The difference between: |
| 48 | + - **Source state** (`products`) |
| 49 | + - **UI state** (search, category, sort) |
| 50 | + - **Derived state** (`visibleProducts`) |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +## Key Concepts Demonstrated |
| 55 | + |
| 56 | +### `useMemo` for derived data |
| 57 | + |
| 58 | +```ts |
| 59 | +const visibleProducts = useMemo(() => { |
| 60 | + let result = products; |
| 61 | + |
| 62 | + if (searchTerm.trim()) { |
| 63 | + result = result.filter((p) => |
| 64 | + p.title.toLowerCase().includes(searchTerm.toLowerCase()), |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + if (category !== "all") { |
| 69 | + result = result.filter((p) => p.category === category); |
| 70 | + } |
| 71 | + |
| 72 | + return [...result].sort(/* sorting logic */); |
| 73 | +}, [products, searchTerm, category, sortBy]); |
| 74 | +``` |
| 75 | + |
| 76 | +- The computation only runs **when its dependencies change** |
| 77 | +- Unrelated state changes do **not** trigger recomputation |
| 78 | +- This scales much better as datasets grow |
| 79 | + |
| 80 | +--- |
| 81 | + |
| 82 | +### Memoizing derived lists (categories) |
| 83 | + |
| 84 | +```ts |
| 85 | +const categories = useMemo(() => { |
| 86 | + const unique = new Set(products.map((p) => p.category)); |
| 87 | + return ["all", ...Array.from(unique)]; |
| 88 | +}, [products]); |
| 89 | +``` |
| 90 | + |
| 91 | +- Categories depend **only** on fetched data |
| 92 | +- They are computed once per data load |
| 93 | +- No recomputation on UI interactions |
| 94 | + |
| 95 | +--- |
| 96 | + |
| 97 | +## Project Structure |
| 98 | + |
| 99 | +``` |
| 100 | +react-data-fetch-memo/ |
| 101 | +├─ src/ |
| 102 | +│ ├─ api/ |
| 103 | +│ │ └─ productsApi.ts |
| 104 | +│ ├─ components/ |
| 105 | +│ │ ├─ Filters.tsx |
| 106 | +│ │ ├─ ProductList.tsx |
| 107 | +│ │ └─ ProductItem.tsx |
| 108 | +│ ├─ hooks/ |
| 109 | +│ │ └─ useProducts.ts |
| 110 | +│ ├─ types/ |
| 111 | +│ │ └─ product.ts |
| 112 | +│ ├─ App.tsx |
| 113 | +│ └─ main.tsx |
| 114 | +├─ public/ |
| 115 | +├─ index.html |
| 116 | +└─ README.md |
| 117 | +``` |
| 118 | + |
| 119 | +--- |
| 120 | + |
| 121 | +## Getting Started |
| 122 | + |
| 123 | +```bash |
| 124 | +npm install |
| 125 | +npm run dev |
| 126 | +# open http://localhost:5173 |
| 127 | +``` |
| 128 | + |
| 129 | +### Production build |
| 130 | + |
| 131 | +```bash |
| 132 | +npm run build |
| 133 | +npm run preview |
| 134 | +``` |
| 135 | + |
| 136 | +--- |
| 137 | + |
| 138 | +## Notes on React StrictMode |
| 139 | + |
| 140 | +- In development, React StrictMode **intentionally double-invokes renders** |
| 141 | +- This is expected and helps surface unsafe side effects |
| 142 | +- **Production builds render only once** |
| 143 | + |
| 144 | +This project is StrictMode-safe. |
| 145 | + |
| 146 | +--- |
| 147 | + |
| 148 | +## Next Steps / Possible Extensions |
| 149 | + |
| 150 | +- Extract filter logic into a custom hook |
| 151 | +- Add pagination or virtualization for large datasets |
| 152 | +- Add unit tests for filtering logic |
| 153 | + |
| 154 | +--- |
| 155 | + |
| 156 | +This project is intentionally small and focused, aiming to demonstrate |
| 157 | +render behavior and memoization patterns rather than feature completeness. |
| 158 | + |
| 159 | +--- |
| 160 | + |
| 161 | +## License |
| 162 | + |
| 163 | +This project is licensed under the **MIT License**. |
0 commit comments