Skip to content

Instantly share code, notes, and snippets.

@balascript
Created July 12, 2020 19:14
Show Gist options
  • Save balascript/363ccab0475f02a7056d3d920b5b7dde to your computer and use it in GitHub Desktop.
Save balascript/363ccab0475f02a7056d3d920b5b7dde to your computer and use it in GitHub Desktop.
function to fetch photos from unsplash API for the given keyword, page, and limit (defaults to 10)
import Unsplash, { toJson } from 'unsplash-js';
const unsplashInstance = new Unsplash({
secret: 'Your secret here',
accessKey: 'Your accessKey here',
});
async function fetchPhotos(keyword, page, limit = 10) {
const photoResult = await unsplashInstance.search
.photos(keyword, page, limit)
.then(toJson);
if (!Array.isArray(photoResult.results)) {
return [];
}
// map the response to a simpler format for Flatlist
const photos = photoResult.results.map(({ urls, id }) => ({
key: id,
uri: urls.small,
}));
return photos;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment