-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathuseGiphy.js
37 lines (30 loc) · 896 Bytes
/
useGiphy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { useState, useEffect } from 'react'
import { getResults } from './api'
/**
* @typedef {import('./api').SearchParams} SearchParams
* @typedef {import('./api').GiphyResult} GiphyResult
*
* @callback SetSearchParams
* @param {SearchParams} searchParams Search parameters
*/
/**
* A custom hook that returns giphy results and a function to search for updated results
* @returns {[GiphyResult[], SetSearchParams]}
*/
const useGiphy = () => {
const [searchParams, setSearchParams] = useState({})
const [results, setResults] = useState([])
useEffect(() => {
const fetchResults = async () => {
try {
const apiResponse = await getResults(searchParams)
setResults(apiResponse.results)
} catch (err) {
console.error(err)
}
}
fetchResults()
}, [searchParams])
return [results, setSearchParams]
}
export default useGiphy