import { useState, useEffect, useMemo } from 'react'
import axiosInstance from '../http/axiosInstance'
export function useDataFetching<T>(url: string, initData: T): T {
  const [data, setData] = useState<T>(initData)

  useEffect(() => {
    const fetchData = async () => {
      const response = await axiosInstance.get(url)
      const result = await response.data
      setData(result)
    }

    fetchData()
  }, [url])
  
  // Memoize the data value
  const memoizedData = useMemo<T>(() => data, [data])
  return memoizedData
}

export default useDataFetching;