import { Query } from "expo-sqlite"; import { useEffect, useState } from "react"; import { executeQuery } from "../services/database"; const useFetch = (query: Query) => { const [isLoading, setIsLoading] = useState(false); const [data, setData] = useState<{[column: string]: any;}[]>([]); const [isEmptyResult, setIsEmptyResult] = useState(undefined); const reFetch = () => { setIsLoading(true); executeQuery(query).then((result) => { if("rows" in result[0]) { setData(result[0]["rows"]); if(result[0]["rows"].length == 0){ setIsEmptyResult(true); } } }).catch((error: any) => { console.error("Fetching data from database has failed: ", error); }).then(() => { setIsLoading(false); }); } useEffect(() => { reFetch(); }, []) return {data, isLoading, reFetch, isEmptyResult}; } export default useFetch;