Ashy Ashcsi
Ashy Ashcsi

Reputation: 1597

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

Upvotes: 0

Views: 2756

Answers (1)

Cassidy
Cassidy

Reputation: 3428

Wouldn't the Random Photo API be the solution you need? https://unsplash.com/documentation#get-a-random-photo

Otherwise if that's not what you need, you could randomize the page you pick from in addition to the photo within that page, perhaps.

Upvotes: 1

Related Questions