Created
April 26, 2020 19:42
-
-
Save amElnagdy/80fecebe72c9ebb2d1fae8c0f9f8aff7 to your computer and use it in GitHub Desktop.
React reusable hooks for fetching data from some API
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
import { useState, useEffect } from "react"; | |
import axios from "axios"; | |
const useResources = (resource) => { | |
const [resources, setResources] = useState([]); | |
useEffect(() => { | |
(async (resource) => { | |
const response = await axios.get( | |
`https://jsonplaceholder.typicode.com/${resource}` | |
); | |
setResources(response.data); | |
})(resource); | |
}, [resource]); | |
return resources; | |
}; | |
export default useResources; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment