RTK QueryBeginner
Fetch List — useGetProductsQuery
কম্পোনেন্টে auto-generated hook ব্যবহার — loading/data — বাংলায়।
4টি অংশবাংলা ভাষায়
src/components/products/product-grid.tsxtsx
tsx
"use client";
import { useGetProductsQuery } from "@/features/orders/ordersService";
import { ProductCard } from "./product-card";
export function ProductGrid() {
// hook auto-created from builder.query
const {
data,
isSuccess,
isLoading,
isError,
refetch,
} = useGetProductsQuery(); // void arg
if (isLoading) {
return <div className="grid gap-4">
{Array.from({ length: 6 }).map((_, i) => (
<ProductCardSkeleton key={i} />
))}
</div>;
}
if (isError) {
return (
<ErrorMessage
title="Product লোড করা যায়নি"
onRetry={() => refetch()}
/>
);
}
if (isSuccess && data.length === 0) {
return <EmptyMessage text="কোনো product নেই" />;
}
return (
<div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
{data?.map((product) => (
<ProductCard key={product.id} product={product} />
))}
</div>
);
}Generated hook fields
| Property | অর্থ |
|---|---|
data | success-এ fetched result |
isLoading | প্রথমবার fetch (কোনো cache নেই) |
isFetching | যেকোনো fetch চলা (background সহ) |
isError | last fetch ব্যর্থ |
error | error object (serialized) |
isSuccess | data present |
refetch | ম্যানুয়ালি আবার fetch |
এক hook-এ অনেক component
একই endpoint+args-সহ বিভিন্ন component-এ use করা হলে একটাই fetch হয় — কারণ cache একই key-তে। এই deduplication-ই RTK Query-র core value।
শেষ প্যারায় নিজে practice করুন — কোড কপি করে নিজের এডিটরে চালিয়ে দেখুন।