[英]pick a random image from unsplash API reactjs
我正在开发一个 reactjs 应用程序,它使用 unsplash API 来更新应用程序的背景图像。 API 的结果如下:
{total: 2330, total_pages: 78, results: Array(30)}
由于 API 每页仅返回 30 张图像,因此我实现了以下逻辑以从 30 张图像列表中检索任何随机图像:
if (typeof response.data !== "undefined") {
const num = Math.floor(Math.random() * 30) + 1;
const url = response.data.results[num].urls.regular;
updateBackground(url);
}
但上述逻辑仅限于从第一页随机抽取一张图片。 如何从总共 2330 张图像中选择随机图像? 有没有办法从结果中排除 total_pages ?
谢谢
随机照片 API 不是您需要的解决方案吗? https://unsplash.com/documentation#get-a-random-photo
否则,如果这不是您所需要的,您可能会随机化您选择的页面以及该页面中的照片。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.