DEV Community

John Ding
John Ding

Posted on

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;

Top comments (0)