Skip to content

Instantly share code, notes, and snippets.

@hosseinmd
Last active November 3, 2020 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hosseinmd/12f248af27fcb62b460d5c0f955dff43 to your computer and use it in GitHub Desktop.
Save hosseinmd/12f248af27fcb62b460d5c0f955dff43 to your computer and use it in GitHub Desktop.
A common way for Fetching data with hooks
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