Skip to content

Instantly share code, notes, and snippets.

@ruucm
Created December 15, 2019 12:09
Show Gist options
  • Save ruucm/f5881dc912170b918c53ee32cda7df70 to your computer and use it in GitHub Desktop.
Save ruucm/f5881dc912170b918c53ee32cda7df70 to your computer and use it in GitHub Desktop.
A simple react hook to get Giphy random image (install use-http hook first)
import { useRef, useState, useEffect } from "react";
import useFetch from "use-http";
const useGiphy = search => {
const [loading, setLoading] = useState(false);
const [data, setData] = useState([]);
const [request, response] = useFetch("http://api.giphy.com/v1");
useEffect(() => {
const getUrl = async () => {
const type = "/gifs";
const searchType = `/random?tag=`;
const apiKey = `&api_key=dc6zaTOxFJmzC`;
const searchTerm = search;
const url = `${type}${searchType}${searchTerm}${apiKey}`;
setLoading(true);
const response = await request.get(url);
console.log("response", response);
setData(response.data.image_url);
setLoading(false);
};
getUrl();
}, []);
return [loading, data];
};
export default useGiphy;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment