This repository has been archived on 2026-04-20. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
interaktive-systeme/hooks/useFetch.ts
2024-01-02 12:56:11 +01:00

30 lines
No EOL
807 B
TypeScript

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 reFetch = () => {
setIsLoading(true);
executeQuery(query).then((result) => {
if("rows" in result[0]) {
setData(result[0]["rows"]);
}
}).catch((error: any) => {
console.error("Fetching data from database has failed: ", error);
}).then(() => {
setIsLoading(false);
});
}
useEffect(() => {
reFetch();
}, [])
return {data, isLoading, reFetch};
}
export default useFetch;