Skip to content

Instantly share code, notes, and snippets.

@jseed
Last active December 1, 2019 04:26
Show Gist options
  • Save jseed/76b9862cdba83afde86a67551970f716 to your computer and use it in GitHub Desktop.
Save jseed/76b9862cdba83afde86a67551970f716 to your computer and use it in GitHub Desktop.
// useApiData.js - general api data loading hook
import { useEffect, useState } from 'react';
import { apiFetch } from '../services/BaseApiService';
export default function useApiData(initialState, endpoint) {
const [loading, setLoading] = useState(false);
const [data, setData] = useState(initialState);
useEffect(() => {
setLoading(true);
apiFetch(endpoint)
.then(res => res.json())
.then(json => {
setData(json.data);
setLoading(false);
});
}, []);
return [loading, data];
}
// useApiCategories.js - category api data loading hook
import { ENDPOINTS } from '../services/BaseApiService';
import useApiData from './useApiData';
export default function useApiCategories() {
return useApiData([], ENDPOINTS.ASSET_CATEGORIES);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment