Day15 Comprehension Answers
Day15 Comprehension Answers
A:
Caching in React (especially with libraries like React Query) refers to storing previously
fetched data so that the app can reuse it without re-fetching from the server. This improves
performance by reducing unnecessary network requests, minimizing latency, and giving the
user faster access to previously viewed or interacted-with data. It also enhances perceived
speed and responsiveness of the app.
2. Write a code snippet using React Query with a type-safe cache key for
fetching product data.
A:
interface Product {
id: number;
name: string;
price: number;
}
const PRODUCT_QUERY_KEY = ['products'] as const;
A:
Caching prevents unnecessary network requests by storing and reusing server responses,
while memoization prevents unnecessary re-renders and recalculations within the app by
storing the results of expensive computations. Together, they improve both data efficiency
and UI performance, allowing large applications to remain responsive and reliable under
heavy usage.
4. Provide a scenario where memoization could actually hurt performance
instead of helping.
A:
Memoization can hurt performance if applied to fast, cheap computations that don’t justify
the cost of storing and tracking memoized values. For example, memoizing a simple map()
over 5 items adds unnecessary overhead without any real benefit. Overusing useMemo or
useCallback may actually slow down rendering due to extra bookkeeping.
A:
Query invalidation tells React Query to refetch specific cached data, ensuring the UI reflects
the most recent server state. After a mutation (like adding, updating, or deleting data), the
affected queries must be invalidated so that stale data isn’t shown to users.
6. Summarize the basic workflow for integrating TanStack Query into a new
React project.
A:
7. How do you use the useQuery hook to fetch data from an API? Write a
simple code example.
A:
return (
<ul>
{data.map((user: any) => (
<li key={user.id}>{user.name}</li>
))}
</ul>
);
};
A:
TanStack Query DevTools let developers visualize query caches, mutations, background
refetches, stale states, and error handling in real time. It helps in debugging issues, improving
performance tuning, and understanding the data lifecycle without needing console logs.
A:
Because without invalidation, your app may continue displaying outdated (stale) data.
Invalidating relevant queries forces React Query to fetch the latest data from the server,
ensuring the UI remains accurate and synchronized with backend state.
A:
Cache invalidation prevents stale data from being shown after a mutation. For instance, if you
add a new product, the product list query must be invalidated so the new product appears in
the UI. This maintains consistency between client-side UI and server data.
11. How can you test edge cases like network errors or concurrent updates
when using React Query or SWR?
A:
You can simulate:
Network failures by disconnecting from the internet or using tools like Chrome
DevTools throttling
Concurrent updates by triggering multiple mutations or updates from different
tabs/devices
Use mocking libraries like MSW (Mock Service Worker) to simulate timeouts,
delays, or error codes
React Query also exposes status, error, and isFetching to handle and display such states
during testing.
12. Why is it important to handle loading and error states in your UI?
A:
Handling loading and error states improves user experience and resilience. Without it, users
may see empty screens, stale data, or unexpected behavior. Clear visual feedback builds trust,
helps users understand app state, and allows recovery from failures gracefully.