Skip to content

Commit c9d0269

Browse files
authored
doc: replace swr with TanStack for next page router tutorial (#337)
1 parent 62b46ae commit c9d0269

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

docs/quick-start/nextjs.mdx

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,11 @@ value (use a complex secret in production and don't check it into git):
218218
ZenStack has built-in support for Next.js and can provide database CRUD services
219219
automagically, so you don't need to write it yourself.
220220

221-
First, install the `@zenstackhq/server` and `@zenstackhq/swr` packages:
221+
First install the `@zenstackhq/server`, `@tanstack/react-query`, and `@zenstackhq/tanstack-query` packages:
222222

223223
```bash
224-
npm install @zenstackhq/server swr
225-
npm install -D @zenstackhq/swr
224+
npm install @zenstackhq/server@latest @tanstack/react-query
225+
npm install -D @zenstackhq/tanstack-query@latest
226226
```
227227

228228
Let's mount it to the `/api/model/[...path]` endpoint. Create a `/src/pages/api/model/[...path].ts`
@@ -253,7 +253,9 @@ Let's enable it by adding the following snippet at the top level to `schema.zmod
253253

254254
```zmodel
255255
plugin hooks {
256-
provider = '@zenstackhq/swr'
256+
provider = '@zenstackhq/tanstack-query'
257+
target = 'react'
258+
version = 'v5'
257259
output = "./src/lib/hooks"
258260
}
259261
```
@@ -274,17 +276,22 @@ First, NextAuth and React Query require context providers to be set up. Let's ad
274276
import { type AppType } from "next/app";
275277
import { type Session } from "next-auth";
276278
import { SessionProvider } from "next-auth/react";
279+
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
277280

278281
import "../styles/globals.css";
279282

283+
const queryClient = new QueryClient();
284+
280285
const MyApp: AppType<{ session: Session | null }> = ({
281286
Component,
282287
pageProps: { session, ...pageProps },
283288
}) => {
284289
return (
285-
<SessionProvider session={session}>
286-
<Component {...pageProps} />
287-
</SessionProvider>
290+
<QueryClientProvider client={queryClient}>
291+
<SessionProvider session={session}>
292+
<Component {...pageProps} />
293+
</SessionProvider>
294+
</QueryClientProvider>
288295
);
289296
};
290297

@@ -306,7 +313,7 @@ import { useCreateUser } from "../lib/hooks";
306313
const Signup: NextPage = () => {
307314
const [email, setEmail] = useState("");
308315
const [password, setPassword] = useState("");
309-
const { trigger: signup } = useCreateUser();
316+
const { mutateAsync: signup } = useCreateUser();
310317

311318
async function onSignup(e: FormEvent) {
312319
e.preventDefault();
@@ -570,9 +577,9 @@ const SigninSignup = () => {
570577

571578
const Posts = () => {
572579
// Post crud hooks
573-
const { trigger: createPost } = useCreatePost();
574-
const { trigger: updatePost } = useUpdatePost();
575-
const { trigger: deletePost } = useDeletePost();
580+
const { mutateAsync: createPost } = useCreatePost();
581+
const { mutateAsync: updatePost } = useUpdatePost();
582+
const { mutateAsync: deletePost } = useDeletePost();
576583

577584
// list all posts that're visible to the current user, together with their authors
578585
const { data: posts } = useFindManyPost({

0 commit comments

Comments
 (0)