S
S
Sol-Mayers2022-02-18 14:15:46
JavaScript
Sol-Mayers, 2022-02-18 14:15:46

Pagination via Rest API?

Good afternoon. Not strong in working with Rest API. In this connection, I have a question - how to navigate through the pages (pagination) in case of receiving data via fetch? We need to somehow make it so that on click the url attribute (entry point) inside fetch changes according to which page we clicked on. I tried to write similar functions, but the url does not change in any way. Tell me, plz. I am attaching part of the code, since the whole code turned out to be very confusing.

const getProducts = async () => {
  const obj = await fetch('https://reqres.in/api/users?page=1');
  const result = await obj.json();
  ...
  ...
  ...
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Nesterov, 2022-02-18
@AlexNest

I tried to write similar functions, but the url does not change in any way.

And how will they change if they are hardcoded (i.e. ' https://reqres.in/api/users?page=1 ' will always send to the first page. You need to generate a link dynamically: for example
function fetch_data(page) { // Номер страницы
  const getProducts = async () => {
    const obj = await fetch('https://reqres.in/api/users?page=' + page); //
    const result = await obj.json();

  }
}
fetch_data(1);
fetch_data(2);
fetch_data(315);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question