#100DAYSOFCODE Day91

#100DAYSOFCODE Day91

Improve Blog Page

Fetch the next page data by React Query

I achieved using less code to fetch the next page using React Query. The key thing is that needs to create a function and put the fetch function inside to pass params. If without adding a function, it would fetch the same page.

const [fetchPage, setFetchPage] = useState(0)
const { data, error, status } = useQuery(["posts", fetchPage], 
() => fetchData(fetchPage), 
{ keepPreviousData: true })

const loadMoreButtonHandler = () => {
    setFetchPage((page) => ++page)
}

Screen Shot 2021-06-02 at 1.17.10 AM.png