import { useState , useCallback } from 'react' ; function useFetchData < Data extends object > ( fetchFn ) { const [ error , setErr ] = useState < any > ( ) ; const [ loading , setLoading ] = useState ( false ) ; const [ data , setData ] = useState < Data > ( ) ; const queryData = useCallback ( ( filterFn ? ) => { setLoading ( true ) ; fetchFn ( ) . then ( res => { const result = filterFn ? filterFn ( res ) : res ; setData ( result ) ; } ) . finally ( ( ) => { setLoading ( false ) ; } ) . catch ( e => setErr ( e ) ) ; } , [ ] ) ; return { fetchResult : { error , data , loading } , queryData } ; } export default useFetchData ;