Hard
You're building a modern e-commerce platform that needs to fetch product data while handling loading and error states elegantly. The product team wants to implement React Suspense for a better user experience. How would you properly implement data fetching with Suspense while ensuring the code follows React 18's best practices and handles cache invalidation?
1import { useState, useTransition, Suspense } from 'react';2import { fetchProductData } from './api';34function ProductCatalog({ categoryId }) {5 const [isPending, startTransition] = useTransition();6 const [products, setProducts] = useState([]);78 const loadProducts = async () => {9 startTransition(async () => {10 const resource = fetchProductData(categoryId);11 // Missing implementation12 });13 };1415 return (16 <div className="product-catalog">17 <button onClick={loadProducts} disabled={isPending}>18 Load Products19 </button>20 <Suspense fallback={<SpinnerComponent />}>21 {/* Component implementation needed */}22 </Suspense>23 </div>24 );25}2627// Resource implementation28const wrapPromise = (promise) => {29 let status = 'pending';30 let result;31 let suspender = promise.then(32 (r) => {33 status = 'success';34 result = r;35 },36 (e) => {37 status = 'error';38 result = e;39 }40 );4142 return {43 // Implementation needed44 };45};