Redux/Redux Toolkit Assignment – 4
Basic Questions
- Install @reduxjs/toolkit and react-redux.
- Install RTK Query as part of Redux Toolkit (npm install @reduxjs/toolkit).
- Explain in code why client-state (Redux) ≠ server-state (API data).
- Set up a base Redux Toolkit store using configureStore().
- Add the api.middleware from RTK Query to the store.
- Create a basic API slice using createApi() with a fakeBaseQuery.
- Define a baseUrl in fetchBaseQuery.
- Create the first endpoint getPosts in createApi().
- Export the auto-generated hook useGetPostsQuery.
- Use <Provider> with store in a React app.
- Use useGetPostsQuery() inside a React component and display data.
- Display a loading message while isLoading is true.
- Display an error message when isError is true.
- Add type safety by defining response shape for API endpoint.
- Show skeleton/loading placeholder while fetching API data.
- Define a createPost mutation endpoint using useCreatePostMutation.
- Call createPost() on form submit with JSON payload {title, body}.
- Create a delete mutation endpoint deletePost.
- Use updatePost mutation (PUT) to edit a record.
- Show mutation states (isLoading, isSuccess, isError) in UI.
Intermediate Questions
- Create an apiSlice with multiple endpoints: getUsers, getPosts, getComments.
- Add caching to queries so repeated requests reuse cached data.
- Invalidate getPosts cache when createPost mutation is successful.
- Use refetch to manually refetch a query response.
- Add tags to an endpoint for smart cache invalidation.
- Show difference between client state update (Redux slice) vs server-state update (RTK Query).
- Handle multiple parallel queries (useGetPostsQuery + useGetUsersQuery).
- Stop polling duplicate requests by using cache.
- Display userinfo fetched with useGetUserQuery(id) dynamically by prop.
- Use polling with query to refresh every 5 seconds.
- Trigger a query on button click using skip option.
- Show how to prefetch data using usePrefetch().
- Fetch paginated API results using query params.
- Handle query parameters dynamically in useGetPostsQuery({page}).
- Fetch dependent data: getPosts then fetch comments of first post.
- Use useCreateUserMutation and update UI immediately on success.
- Show optimistic updates: update UI before mutation success response.
- Add error handling UI for failed deletePost mutation.
- Chain two actions: create a post → invalidate cache → refetch list.
- Store mutation result response to local Redux slice for offline support.
Advanced Questions
- Build a blog app that fetches posts (GET), adds new post (POST), edits (PUT), and deletes (DELETE) using RTK Query.
- Add infinite scrolling with paginated API using useGetPostsQuery({page}).
- Implement real-time polling of data every 3 seconds for live updates (e.g., live comments feed).
- Create a User Profile Page with separate queries (getUser, getUserPosts, getUserComments).
- Handle global error states with an RTK Query onError handler.
- Add a custom baseQuery with re-authentication flow (retry when token expires).
- Integrate RTK Query inside a Redux slice to combine client state + API state.
- Use invalidateTags smartly across multiple endpoints (Posts, Users, Comments).
- Show parallel + conditional queries: fetch posts only if user exists.
- Build a mini E-commerce app with product fetch query + cart mutations (addToCart, removeFromCart) handled via RTK Query.