samedi 30 mai 2020

pick a random image from unsplash API reactjs

I am developing a reactjs application which uses unsplash API to update background images of the application. The result from the API is as below:

{total: 2330, total_pages: 78, results: Array(30)}

As the API gives back 30 images only per page, I have implemented the following logic to retrieve any random image from the list of 30 images:

        if (typeof response.data !== "undefined") {
          const num = Math.floor(Math.random() * 30) + 1;
          const url = response.data.results[num].urls.regular;
          updateBackground(url);
        }

But the above logic is limited to picking up a random image from the first page only. How do I pick a random image from the total of 2330 images ? Is there a way to exclude total_pages from the result ?

Thanks




Aucun commentaire:

Enregistrer un commentaire