1

I need to create pagination in my site. I'm getting data like this from REST API:

{
    "count": 4,
    "next": "http://127.0.0.1:8000/api/software/?p=2",
    "previous": null,
    "results": [
        {
            "id": 4,
            "category": {
                "id": 10,
                "parent": {
                    "id": 2,
                    "name": "Name",
                    "slug": "Slug",
                    "img": "Image"
                },
                "name": "Name",
                "slug": null,
                "img": ""
            },
            "title": "Title",
            "content": "Content",
            "image": "Image",
            "pub_date": "2022-02-02",
            "counter": 0,
            "file": "File",
            "in_archive": false
        },
        {
            "id": 3,
            "category": {
                "id": 11,
                "parent": {
                    "id": 2,
                    "name": "Name",
                    "slug": "Slug",
                    "img": "Image"
                },
                "name": "Name",
                "slug": null,
                "img": ""
            },
            "title": "Title",
            "content": "Content",
            "image": "Image",
            "pub_date": "2022-02-02",
            "counter": 0,
            "file": "File",
            "in_archive": false
        }
    ]
}

In React I'm trying to get the data like this:

const [currentPage, setCurrentPage] = useState(1);
const [count, setCount] = useState(null);
const [nextPage, setNextPage] = useState(null);
const [previousPage, setPreviousPage] = useState(null);
const [valid, setValid] = useState(false);

useEffect(() => {
  fetch(`http://127.0.0.1:8000/api/software/?p=${currentPage}`)
      .then(response => response.json())
      .then(data => {
        setCount(data.count);
        setNextPage(data.next);
        setPreviousPage(data.previous);
        setValid(true);
      })
}, []);

But I have no idea how it must be and how to use it in React Component, I've not done that before.

How it has to be in React?

1 Answer 1

3

Currently your useEffect hook has no dependency. It will only run on mount and unmount. Instead add your currentPage as a dependency for your useEffect Hook.

Whenever the currentPage changes useEffect hook will run and the data will be fetched.

useEffect(() => {
          fetch(`http://127.0.0.1:8000/api/software/?p=${currentPage}`)
              .then(response => response.json())
              .then(data => {
                setCount(data.count);
                setNextPage(data.next);
                setPreviousPage(data.previous);
                setValid(true);
              })
        }, [currentPage]); //Added Dependency

To load the next page simply increment your currentPage

const incrementPageNumber = () => setCurrentPage(prevPage => prevPage + 1);

To load the previous page simply decrement your currentPage

const decrementPageNumber = () => {
  if(currentPage <= 1) return; 
  setCurrentPage(prevPage => prevPage - 1);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for answer, it helped. But I have a question: do I need to add Route with the page number or not?
For this approach, there is no need for any additional routes
I have written a small sandbox for you. Refer - codesandbox.io/s/gallant-meninsky-1i50j?file=/src/App.tsx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.