added useApi and useApiQuery to work seamlessly with SWR (tbc)

This commit is contained in:
dswbx
2024-12-12 10:37:52 +01:00
parent 43ec075a32
commit 9d9aa7b7a5
10 changed files with 72 additions and 24 deletions

View File

@@ -1,5 +1,6 @@
import AppShellAccordionsTest from "ui/routes/test/tests/appshell-accordions-test";
import SwaggerTest from "ui/routes/test/tests/swagger-test";
import SWRAndAPI from "ui/routes/test/tests/swr-and-api";
import { Route, useParams } from "wouter";
import { Empty } from "../../components/display/Empty";
import { Link } from "../../components/wouter/Link";
@@ -37,7 +38,8 @@ const tests = {
EntityFieldsForm,
FlowsTest,
AppShellAccordionsTest,
SwaggerTest
SwaggerTest,
SWRAndAPI
} as const;
export default function TestRoutes() {

View File

@@ -0,0 +1,20 @@
import { useState } from "react";
import { useApiQuery } from "ui/client";
import { Scrollable } from "ui/layouts/AppShell/AppShell";
export default function SWRAndAPI() {
const [enabled, setEnabled] = useState(false);
const { data, error, isLoading } = useApiQuery(({ data }) => data.readMany("posts"), {
enabled,
revalidateOnFocus: true
});
return (
<Scrollable>
<button onClick={() => setEnabled((p) => !p)}>{enabled ? "disable" : "enable"}</button>
{error && <div>failed to load</div>}
{isLoading && <div>loading...</div>}
{data && <pre>{JSON.stringify(data, null, 2)}</pre>}
</Scrollable>
);
}