Redux/Redux Toolkit Assignment – 5
Basic Questions
- Run the same useGetPostsQuery() hook twice in two components and verify caching prevents duplicate API calls.
- Add providesTags: [‘Post’] to a query and check cache tagging in DevTools.
- Invalidate cache manually using invalidateTags after a createPost mutation.
- Refetch all posts by calling refetch() on a query result.
- Set a query to automatically refetch when a window regains focus (refetchOnFocus: true).
- Enable background polling with pollingInterval: 5000 for a live feed.
- Demonstrate cache being replaced when calling useGetUserQuery(1) vs useGetUserQuery(2).
- Configure cache lifetime using keepUnusedDataFor: 30 seconds.
- Create an endpoint that fetches comments and tie it to the same cache tag as posts.
- Observe how mutation invalidation automatically updates cached queries.
- Show how errors appear with isError when an API call fails.
- Display API error messages in UI using error.error.
- Add fallback UI rendering when a query returns an error.
- Write an optimistic update for adding a post and then roll back if the server fails.
- Apply optimistic update for deleting an item (remove from UI first, then restore on failure).
- Keep Redux state minimal by storing only IDs in posts state instead of full objects.
- Write a normalized state example for users with entities + ids.
- Use a selector to fetch a single post by id.
- Organize state into separate userSlice and postSlice.
- Document file structure for modular Redux (/slices, /store, /api).
Intermediate Questions
- Manually invalidate only the cache for a specific post (invalidateTags: (result, error, id) => [{ type: ‘Post’, id }]).
- Use dependent queries (fetch post → then fetch comments with the same ID).
- Show cache sharing when two components use the same query with identical params.
- Demonstrate using prefetch() from api.util to warm up cache before navigation.
- Add a query that fetches paginated data and caches each page separately.
- Cancel an ongoing request using the abort() signal from RTK Query.
- Persist RTK Query cache into localStorage.
- Add error boundary component that wraps all RTK Query fetched components.
- Use onError middleware to globally handle and log query errors.
- Implement optimistic update for liking a post (increment likes locally, rollback if error).
- Build UI that shows optimistic comment-added immediately, then confirm with server.
- Demonstrate handling network error with a retry option button in UI.
- Handle unauthorized API call (401) by dispatching logout from rejectWithValue.
- Show toast notifications on mutation success/failure.
- Split a large Redux store into multiple slices (auth, posts, comments, ui).
- Use combineReducers() with multiple slices in a scalable structure.
- Define selectors for memoized access to usersById.
- Create a normalized entity adapter with createEntityAdapter().
- Compare normalized vs denormalized state for performance.
- Modularize slice files into /features/auth/authSlice.js, /features/posts/postsSlice.js.
Advanced Questions
- Create a real-time chat with polling every 2 seconds and automatic invalidation when sending a message.
- Implement a cache invalidation strategy that only deletes stale comments after 60s of no usage.
- Build nested optimistic updates: Add a post optimistically, then optimistically add a comment before server returns data.
- Combine useQuery + useMutation to perform inline updates for partial cached objects.
- Implement global error handler middleware that retries failed queries up to 3 times.
- Refactor a monolithic Redux store into feature-based modular slices with RTK best practices.
- Build a normalized large-scale store managing authors, posts, comments with selectors & entity adapters.
- Create a scalable dashboard app architecture using slices + apiSlice + RTK Query.
- Implement lazy-loaded slices for code-splitting in a large app.
- Document and enforce best practices: (minimal state, normalized structure, selectors, and modular file organization) inside a project.