Last active
December 1, 2019 04:26
-
-
Save jseed/76b9862cdba83afde86a67551970f716 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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