Skip to content

Commit 4aa8754

Browse files
authored
Merge pull request #50 from SpaceyaTech/moredocs
Update documentation and testing guidelines for TANSTACK-QUERY and TANSTACK-ROUTER
2 parents 31b7e9c + 4a9735a commit 4aa8754

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ tsconfig.node.tsbuildinfo
2828
/blob-report/
2929
/playwright/.cache/
3030
tsconfig.app.tsbuildinfo
31+
codealike.json

docs/TANSTACK-QUERY.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# tips
2+
3+
- prefer `useSuspenseQuery` over `useQuery`
4+
- prefer defining `queryOptions` over inlinignthem into the query hooks
5+
6+
```tsx
7+
import { queryOptions,useSuspenseQuery } from "@tanstack/react-query";
8+
//users/queryOptions.ts
9+
// this is re-usable and decltters your components
10+
export const userQueryOptions = queryOptions({
11+
queryKey: ["user"],
12+
queryFn: () => {
13+
...
14+
}
15+
})
16+
// users.tsx
17+
const query = useSuspenseQuery(userQueryOptions);
18+
```
19+
- wrap the data fetching components with `Suspense` component
20+
21+
```tsx
22+
// users page
23+
<div>
24+
<header>Users page</headers>
25+
<Suspense fallback={<div>Loading users</div>}>
26+
<Users />
27+
</Suspense>
28+
</div>
29+
```
30+
31+
run `p/mpm run page users` to scafold a page with these best practices
32+

docs/TANSTACK-ROUTER.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
- anything under the src/routes folder is a route
2+
3+
ex
4+
5+
- `src/routes/index.tsx` ->` /`
6+
- `src/routes/profile.tsx` or `src/routes/profile/index.tsx` -> `/profile/`
7+
8+
Dynamic routes
9+
10+
- `src/routes/users/$oneuser/index.tsx` -> `/users/$oneuser`
11+
12+
inside here you'll have access to the `oneuser` param
13+
14+
```tsx
15+
// src/routes/users/$user/index.tsx
16+
import { createFileRoute, useParams } from '@tanstack/react-router'
17+
18+
export const Route = createFileRoute('/users/$user')({
19+
component:OneUserComponent,
20+
})
21+
functon OneUserComponent() {
22+
const {user} = useParams({
23+
from:"/users/$user"
24+
})
25+
return (
26+
<div>
27+
<h1>{user}</h1>
28+
</div>
29+
)
30+
}
31+
```
32+
33+
if you nest multiple dynamic routes then you can access them in the same way
34+
35+
```tsx
36+
// src/routes/users/$user/friends/$friend/index.tsx
37+
import { createFileRoute, useParams } from '@tanstack/react-router'
38+
39+
export const Route = createFileRoute('/users/$user/friends/$friend/')({
40+
component: RouteComponent,
41+
})
42+
43+
function RouteComponent() {
44+
const { friend,user } = useParams({
45+
from: "/users/$user/friends/$friend/",
46+
});
47+
return (
48+
<div>
49+
<h1>{user}</h1>
50+
<h1>{friend}</h1>
51+
</div>
52+
);
53+
}
54+
55+
56+
```
57+
58+
you can also define search params ,validate them and auth guard
59+
60+
```tsx
61+
import {
62+
createFileRoute,
63+
redirect,
64+
useNavigate,
65+
useSearch,
66+
} from "@tanstack/react-router";
67+
import { z } from "zod";
68+
const searchparams = z.object({
69+
// make it optional if it won't always be used
70+
page: z.number().optional(),
71+
});
72+
73+
export const Route = createFileRoute("/users/")({
74+
component: RouteComponent,
75+
// declare and validate your search params here
76+
// this page should alwatys have /users?page=1
77+
validateSearch: (search) => searchparams.parse(search),
78+
// this is how you auth guard routes (only allow logged in users here )
79+
async beforeLoad(ctx) {
80+
const viewer = ctx.context?.viewer;
81+
if (!viewer?.record) {
82+
throw redirect({
83+
to: "/auth",
84+
// this search params will be used to redirect you back to this page once you log in
85+
search: { returnTo: ctx.location.pathname },
86+
});
87+
}
88+
},
89+
});
90+
91+
function RouteComponent() {
92+
const { page } = useSearch({
93+
from: "/users/",
94+
});
95+
const navigate = useNavigate({
96+
from: "/users",
97+
});
98+
return (
99+
<div>
100+
{page}
101+
<button onClick={() => navigate({ search: { page: (page ?? 0) + 1 } })}>
102+
next page
103+
</button>
104+
</div>
105+
);
106+
}
107+
108+
```

docs/TESTING.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Testing
22

3+
>[!NOTE]
4+
> Prefer e2e (playwright) fro testing the ui instead of unit tests. and uase the tooling like recording of tests and locators.
5+
> use unit tests for units of logic and functions only
6+
7+
8+
39
```sh
410
npm run test
511
```

0 commit comments

Comments
 (0)