John Ding
Posted on January 11, 2024
const useFetchLists = (
url = "",
currentPage = 1,
selectedPageSize = 10,
keyword = ""
) => {
const [items, setItems] = useState([]);
const [loading, setloading] = useState(false);
const [totalPage, setTotalPage] = useState(1);
const [totalItemCount, setTotalItemCount] = useState(0);
const fetchListData = useCallback(async () => {
try {
setloading(true);
await Adapter.get(
${url}?pageNumber=${currentPage}&pageSize=${selectedPageSize}&keyword=${keyword}
,
{}
).then((response) => {
setItems(response.data.items);
setTotalPage(response.data.totalPages);
setTotalItemCount(response.data.totalItems);
});
} catch (err) {
} finally {
setloading(false);
}
}, [url, currentPage, selectedPageSize, keyword]);
useEffect(() => {
fetchListData();
}, [fetchListData]);
return {
data: [items, loading, totalPage, totalItemCount],
refetch: fetchListData
};
};
export default useFetchLists;
Posted on January 11, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
September 8, 2022