adding user hooks to replace api

This commit is contained in:
Elijah Lucian
2021-07-12 21:43:21 -06:00
parent 5e5c539ee7
commit 1af8209b99
11 changed files with 97 additions and 34 deletions
+27
View File
@@ -0,0 +1,27 @@
import { useCallback, useEffect, useRef, useState } from 'react'
import { useAppContext } from '../../contexts/AppContext'
type Res<T> = {
data: T
}
export const useGet = <T>(path: string) => {
const appContext = useRef(useAppContext())
const [data, setData] = useState<T | null>(null)
const get = useCallback(async () => {
// if (appContext.current.mock) return setData(mockGet[path]?.() || null)
const data = await appContext.current.get<Res<T>>(path)
if (!data) return
setData(data.data)
}, [path])
useEffect(() => {
get()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
return { data, get }
}