Last active
November 3, 2020 18:03
-
-
Save hosseinmd/12f248af27fcb62b460d5c0f955dff43 to your computer and use it in GitHub Desktop.
A common way for Fetching data with hooks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function App() { | |
const [data, setData] = useState([]); | |
const [isLoading, setLoading] = useState(false); | |
const [error, setError] = useState(''); | |
const [query, setQuery] = useState('a query for search'); | |
useEffect(() => { | |
const fetchData = async () => { | |
setLoading(true) | |
try { | |
const result = await axios( | |
`http://hn.algolia.com/api/v1/search?query=${query}`, | |
); | |
setData(result.data); | |
} catch (err) { | |
setError(err.message) | |
} | |
setLoading(false) | |
}; | |
fetchData(); | |
}, [query]); | |
return ( | |
... | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment