Home
E-commerce code snippets in JavaScript?
Posted on March 7, 2021
// setup pages control for every table
const [pageTable, setPageTable] = useState(1);
// setup data for every table
const [items, setItems] = useState<any>([]);
const [totalResults, setTotal] = useState(0);
// pagination setup
const resultsPerPage = 10;
// pagination change control
const onPageChangeTable = (p: number) => {
setPageTable(p);
};
useEffect(() => {
fetchItems((response: any) => {
const { status, message } = response;
if (status === "info") {
const items = response.data;
setItems(
items.slice(
(pageTable - 1) * resultsPerPage,
pageTable * resultsPerPage
)
);
setTotal(items.length);
setHue("");
setMessage("");
}
if (status === "error") {
setMessage(message);
setHue("red");
}
});
// eslint-disable-next-line
}, [pageTable]);
<Pagination
totalResults={totalResults}
resultsPerPage={resultsPerPage}
onChange={onPageChangeTable}
label="Items"
/>