Skip to content

Instantly share code, notes, and snippets.

@amElnagdy
Created April 26, 2020 19:42
Show Gist options
  • Save amElnagdy/80fecebe72c9ebb2d1fae8c0f9f8aff7 to your computer and use it in GitHub Desktop.
Save amElnagdy/80fecebe72c9ebb2d1fae8c0f9f8aff7 to your computer and use it in GitHub Desktop.
React reusable hooks for fetching data from some API
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