Created
July 12, 2020 19:14
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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