State Management · Middle · Question 34 of 75
How do you make an RTK Query mutation refetch the post list?
getPosts: build.query({ query: () => "posts", providesTags: ["Post"] }),
addPost: build.mutation({ query: (body) => ({ url: "posts", method: "POST", body }) }),- ✓Correct answer: Add
invalidatesTags: ['Post']toaddPost - 2Call
getPosts()inside the mutation'squery - 3Add
refetchOnMutation: truetogetPosts
Explanation
Tags link the cache to mutations. For finer control, tag items by id ({ type: 'Post', id }) plus a 'LIST' tag, so editing one post refetches only what's affected.