Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const useApiResponse (endpoint, options) {
- const [ loading, setLoading ] = useState(false)
- const [ results, setResults ] = useState([])
- useEffect(() => {
- setLoading(true)
- api.get(endpoint, options).then(res => {
- setResults(res)
- setLoading(false)
- })
- }, [ endpoint, options ])
- return { loading, results }
- }
- const useSearch (query) {
- return useApiResponse('/search?q=' + query)
- }
- const Component = () => {
- const [ query, setQuery ] = useState('')
- const { loading, results } = useSearch(query)
- return <div>{ searchResults }</div>
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement