Skip to main content
Refine AI
Version: 3.xx.xx

filtering-live-preview

localhost:3000/products
Live previews only work with the latest documentation.
import { useState } from "react";
import { useInfiniteList, HttpError } from "@pankod/refine-core";

interface IProduct {
id: number;
name: string;
material: string;
}

const ProductList: React.FC = () => {
const [value, setValue] = useState("Cotton");

const {
data,
isLoading,
isError,
hasNextPage,
fetchNextPage,
isFetchingNextPage
} = useInfiniteList<IProduct, HttpError>({
resource: "products",
config: {
filters: [
{
field: "material",
operator: "eq",
value,
},
],
},
});

if (isLoading) {
return <div>Loading...</div>;
}

if (isError) {
return <div>Something went wrong!</div>;
}

const allPages = [].concat(...(data?.pages ?? []).map((page) => page.data));

return (
<div>
<span> material: </span>
<select value={value} onChange={(e) => setValue(e.target.value)}>
{["Cotton", "Bronze", "Plastic"].map((material) => (
<option key={material} value={material}>
{material}
</option>
))}
</select>

<ul>
{allPages.map((product) => (
<li key={product.id}>
{product.name} - ({product.material})
</li>
))}
</ul>

{
hasNextPage && (
<button
onClick={() => fetchNextPage()}
disabled={isFetchingNextPage}
>
{isFetchingNextPage ? "Loading more..." : "Load More" }
</button>
)
}
</div>
);
};