Skip to content

Commit 4e98360

Browse files
authored
docs: Add react router examples (#166)
1 parent 6bb51ca commit 4e98360

35 files changed

+2268
-49
lines changed

biome.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
"examples/nextjs-app/.next",
1414
"examples/tanstack-router-app/openapi",
1515
"examples/tanstack-router-app/src/routeTree.gen.ts",
16+
"examples/react-router-6-app/openapi",
17+
"examples/react-router-7-app/openapi",
1618
"docs/.astro"
1719
]
1820
},
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
title: React Router 6 Example
3+
description: A simple example of using React Router 6 with OpenAPI React Query Codegen.
4+
---
5+
6+
Example of using React Router 6 can be found in the [`examples/react-router-6-app`](https://github.com/7nohe/openapi-react-query-codegen/tree/main/examples/react-router-6-app) directory of the repository.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
title: React Router 7 Example
3+
description: A simple example of using React Router 7 with OpenAPI React Query Codegen.
4+
---
5+
6+
Example of using React Router 7 can be found in the [`examples/react-router-7-app`](https://github.com/7nohe/openapi-react-query-codegen/tree/main/examples/react-router-7-app) directory of the repository.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Vite + React + TS</title>
8+
</head>
9+
<body>
10+
<div id="root"></div>
11+
<script type="module" src="/src/main.tsx"></script>
12+
</body>
13+
</html>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "@7nohe/react-router-6-app",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "run-p dev:mock dev:client",
8+
"dev:client": "vite --clearScreen=false",
9+
"dev:mock": "prism mock ../petstore.yaml --dynamic",
10+
"build": "tsc && vite build",
11+
"preview": "vite preview",
12+
"generate:api": "rimraf ./openapi && node ../../dist/cli.mjs -i ../petstore.yaml --format=biome --lint=biome",
13+
"test:generated": "tsc -p ./tsconfig.json --noEmit"
14+
},
15+
"dependencies": {
16+
"@hey-api/client-axios": "^0.2.7",
17+
"@tanstack/react-query": "^5.59.13",
18+
"@tanstack/react-query-devtools": "^5.32.1",
19+
"axios": "^1.7.7",
20+
"form-data": "~4.0.0",
21+
"react": "^18.3.1",
22+
"react-dom": "^18.3.1",
23+
"react-router-dom": "^6.27.0"
24+
},
25+
"devDependencies": {
26+
"@biomejs/biome": "^1.7.2",
27+
"@stoplight/prism-cli": "^5.5.2",
28+
"@types/react": "^18.3.1",
29+
"@types/react-dom": "^18.2.18",
30+
"@vitejs/plugin-react": "^4.2.1",
31+
"npm-run-all": "^4.1.5",
32+
"typescript": "^5.4.5",
33+
"vite": "^5.0.12"
34+
}
35+
}
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#root {
2+
max-width: 1280px;
3+
margin: 0 auto;
4+
padding: 2rem;
5+
text-align: center;
6+
}
7+
8+
.logo {
9+
height: 6em;
10+
padding: 1.5em;
11+
will-change: filter;
12+
}
13+
.logo:hover {
14+
filter: drop-shadow(0 0 2em #646cffaa);
15+
}
16+
.logo.react:hover {
17+
filter: drop-shadow(0 0 2em #61dafbaa);
18+
}
19+
20+
@keyframes logo-spin {
21+
from {
22+
transform: rotate(0deg);
23+
}
24+
to {
25+
transform: rotate(360deg);
26+
}
27+
}
28+
29+
@media (prefers-reduced-motion: no-preference) {
30+
a:nth-of-type(2) .logo {
31+
animation: logo-spin infinite 20s linear;
32+
}
33+
}
34+
35+
.card {
36+
padding: 2em;
37+
}
38+
39+
.read-the-docs {
40+
color: #888;
41+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import "./App.css";
2+
import type { QueryClient } from "@tanstack/react-query";
3+
import { useState } from "react";
4+
import { type LoaderFunctionArgs, useLoaderData } from "react-router-dom";
5+
import { UseFindPetsKeyFn, useAddPet } from "../openapi/queries";
6+
import { ensureUseFindPetsData } from "../openapi/queries/ensureQueryData";
7+
import { useFindPetsSuspense } from "../openapi/queries/suspense";
8+
import { queryClient } from "./queryClient";
9+
10+
export const loader =
11+
(queryClient: QueryClient) => async (_: LoaderFunctionArgs) => {
12+
const queryParameters = {
13+
query: { tags: [], limit: 10 },
14+
};
15+
16+
await ensureUseFindPetsData(queryClient, {
17+
query: { tags: [], limit: 10 },
18+
});
19+
return queryParameters;
20+
};
21+
22+
export function Compoment() {
23+
const queryParameters = useLoaderData() as Awaited<
24+
ReturnType<ReturnType<typeof loader>>
25+
>;
26+
27+
const { data, error, refetch } = useFindPetsSuspense(queryParameters);
28+
29+
const { mutate: addPet, isError } = useAddPet();
30+
31+
const [text, setText] = useState<string>("");
32+
const [errorText, setErrorText] = useState<string>();
33+
34+
if (error)
35+
return (
36+
<div>
37+
<p>Failed to fetch pets</p>
38+
<button type="button" onClick={() => refetch()}>
39+
Retry
40+
</button>
41+
</div>
42+
);
43+
44+
return (
45+
<div className="App">
46+
<h1>Pet List</h1>
47+
<input
48+
type="text"
49+
value={text}
50+
placeholder="Type pet name"
51+
onChange={(e) => setText(e.target.value)}
52+
/>
53+
<button
54+
type="button"
55+
onClick={() => {
56+
addPet(
57+
{
58+
body: { name: text },
59+
},
60+
{
61+
onSuccess: () => {
62+
queryClient.invalidateQueries({
63+
queryKey: UseFindPetsKeyFn(queryParameters),
64+
});
65+
console.log("success");
66+
},
67+
onError: (error) => {
68+
console.log(error.message);
69+
setErrorText(`Error: ${error.message}`);
70+
},
71+
},
72+
);
73+
}}
74+
>
75+
Create a pet
76+
</button>
77+
{isError && (
78+
<p
79+
style={{
80+
color: "red",
81+
}}
82+
>
83+
{errorText}
84+
</p>
85+
)}
86+
<ul>
87+
{Array.isArray(data) &&
88+
data?.map((pet, index) => (
89+
<li key={`${pet.id}-${index}`}>{pet.name}</li>
90+
))}
91+
</ul>
92+
</div>
93+
);
94+
}
Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)