Redux & Redux Toolkit Course
Redux & Redux Toolkit
/
Advanced

Tag Invalidation

Definition

The automated system RTK Query uses to know exactly WHEN to delete stale cache data and refetch it from the server. Queries 'provide' tags, and Mutations 'invalidate' tags.

Explain Like I'm New

You fetch a list of Posts. RTK Query labels that data with a sticky note: `['Post']`. Then, you submit a Mutation to DELETE a post. You tell the Mutation: 'When you succeed, tear up the `['Post']` sticky note'. RTK Query sees the note torn up, realizes its cache is outdated, and automatically fetches the fresh list of Posts in the background.

Real World Example

Without Tag Invalidation, if you delete an item from a list, the item will disappear from the server, but your screen will still show it because it's stuck in the cache. Tags fix this automatically.

Common Use Cases

  • •Keeping the UI perfectly synced with the database
  • •Automated refetching

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • Which endpoints `provideTags`: Queries or Mutations?

intermediate

  • What happens immediately after a Mutation invalidates a Tag?

Flash Cards

Question

Which provides?

Click to reveal answer
Answer

Queries `providesTags` (They label the data they fetched). Mutations `invalidatesTags` (They destroy the label when they change data).

Question

What happens?

Click to reveal answer
Answer

Any active Queries on the screen that provided that specific Tag will immediately and automatically trigger a background refetch to get the fresh data from the server.