Advertisement
smhdale

Hooks search example

Jun 2nd, 2019
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const useApiResponse (endpoint, options) {
  2.   const [ loading, setLoading ] = useState(false)
  3.   const [ results, setResults ] = useState([])
  4.  
  5.   useEffect(() => {
  6.     setLoading(true)
  7.     api.get(endpoint, options).then(res => {
  8.       setResults(res)
  9.       setLoading(false)
  10.     })
  11.   }, [ endpoint, options ])
  12.  
  13.   return { loading, results }
  14. }
  15.  
  16. const useSearch (query) {
  17.   return useApiResponse('/search?q=' + query)
  18. }
  19.  
  20. const Component = () => {
  21.   const [ query, setQuery ] = useState('')
  22.   const { loading, results } = useSearch(query)
  23.  
  24.   return <div>{ searchResults }</div>
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement