Implement refetch with axis

dingzhanjun

John Ding

Posted on January 11, 2024

Implement refetch with axis

https://stackoverflow.com/questions/71191924/how-can-i-re-trigger-a-custom-hook-inside-my-react-component-when-its-states-ha

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;

💖 💪 🙅 🚩
dingzhanjun
John Ding

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