Skip to content

Instantly share code, notes, and snippets.

@ruucm
Last active December 15, 2019 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruucm/4c03f124523e4c515312db1d44c1f521 to your computer and use it in GitHub Desktop.
Save ruucm/4c03f124523e4c515312db1d44c1f521 to your computer and use it in GitHub Desktop.
A simple react hook to get unsplash radom image without API
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