Last active
December 15, 2019 12:09
-
-
Save ruucm/4c03f124523e4c515312db1d44c1f521 to your computer and use it in GitHub Desktop.
A simple react hook to get unsplash radom image without API
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 { useRef, useState, useEffect } from "react"; | |
const useUnsplash = search => { | |
const [loading, setLoading] = useState(false); | |
const [data, setData] = useState([]); | |
useEffect(() => { | |
const getUrl = async () => { | |
var currentSize = 800; | |
const baseUrl = "https://source.unsplash.com"; | |
const route = search === "" ? "/random" : `/featured`; | |
const url = `${baseUrl}${route}/${currentSize}x${currentSize}?${search}`; | |
setLoading(true); | |
const response = await fetch(url); | |
setData(url); | |
setLoading(false); | |
console.log("response", response); | |
}; | |
getUrl(); | |
}, []); | |
return [loading, data]; | |
}; | |
export default useUnsplash; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment