RTK QueryAdvanced
Tags — providesTags / invalidatesTags
Tag system-এর মাধ্যমে cache invalidation — বাংলায়।
5টি অংশবাংলা ভাষায়
Tags RTK Query-এর 'invalidation ভাষা'। কোনো mutation সফল হলে invalidatesTags বলে দেয় কোন cache পুরনো — RTK Query তখন ওইসব query re-fetch করে।
Tag-সহ endpointts
ts
tagTypes: ["Product", "Order"],
getProducts: builder.query<Product[], void>({
query: () => "products",
providesTags: (result) =>
result
? [
...result.map((p) => ({ type: "Product" as const, id: p.id })),
{ type: "Product", id: "LIST" },
]
: [{ type: "Product", id: "LIST" }],
}),
getProduct: builder.query<Product, string>({
query: (id) => "products/" + id,
providesTags: (_result, _err, id) => [{ type: "Product", id }],
}),
deleteProduct: builder.mutation<void, string>({
query: (id) => ({ url: "products/" + id, method: "DELETE" }),
// যেকোনো Product tag-সহ query (LIST/detail) সব invalid
invalidatesTags: [{ type: "Product", id: "LIST" }, "Product"],
}),
updateProduct: builder.mutation<Product, { id: string; body: Partial<Product> }>({
query: ({ id, body }) => ({ url: "products/" + id, method: "PATCH", body }),
invalidatesTags: (_result, _err, { id }) => [
{ type: "Product", id },
{ type: "Product", id: "LIST" },
],
}),Tag-ID gist
{ type: "Product", id: p.id }→ নির্দিষ্ট item-এর cache{ type: "Product", id: "LIST" }→ list-নির্দিষ্ট tag (ধরে নিলেন প্রতিটি GET list এ tag)"Order"(id ছাড়া) → array-শর্টহ্যান্ড — সব Order tag- invalidatesTag
[LIST + type]→ list ও সব detail দুটোই invalid
Tag kn-কেন?
Tags-এর মূল কথা — সার্ভার কী প্রভাব ফেলবে সেটাই index। একটি update list-ও invalidate করে detail cache — নির্ভরশীল সব query একসাথে updated থাকে।
শেষ প্যারায় নিজে practice করুন — কোড কপি করে নিজের এডিটরে চালিয়ে দেখুন।