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
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { client } from "../openapi/requests/services.gen";
2+
3+
client.setConfig({
4+
baseUrl: "http://localhost:4010",
5+
throwOnError: true,
6+
});
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
:root {
2+
font-family: Inter, Avenir, Helvetica, Arial, sans-serif;
3+
font-size: 16px;
4+
line-height: 24px;
5+
font-weight: 400;
6+
7+
color-scheme: light dark;
8+
color: rgba(255, 255, 255, 0.87);
9+
background-color: #242424;
10+
11+
font-synthesis: none;
12+
text-rendering: optimizeLegibility;
13+
-webkit-font-smoothing: antialiased;
14+
-moz-osx-font-smoothing: grayscale;
15+
-webkit-text-size-adjust: 100%;
16+
}
17+
18+
a {
19+
font-weight: 500;
20+
color: #646cff;
21+
text-decoration: inherit;
22+
}
23+
a:hover {
24+
color: #535bf2;
25+
}
26+
27+
body {
28+
margin: 0;
29+
display: flex;
30+
place-items: center;
31+
min-width: 320px;
32+
min-height: 100vh;
33+
}
34+
35+
h1 {
36+
font-size: 3.2em;
37+
line-height: 1.1;
38+
}
39+
40+
button {
41+
border-radius: 8px;
42+
border: 1px solid transparent;
43+
padding: 0.6em 1.2em;
44+
font-size: 1em;
45+
font-weight: 500;
46+
font-family: inherit;
47+
background-color: #1a1a1a;
48+
cursor: pointer;
49+
transition: border-color 0.25s;
50+
}
51+
button:hover {
52+
border-color: #646cff;
53+
}
54+
button:focus,
55+
button:focus-visible {
56+
outline: 4px auto -webkit-focus-ring-color;
57+
}
58+
59+
@media (prefers-color-scheme: light) {
60+
:root {
61+
color: #213547;
62+
background-color: #ffffff;
63+
}
64+
a:hover {
65+
color: #747bff;
66+
}
67+
button {
68+
background-color: #f9f9f9;
69+
}
70+
}
71+
72+
input {
73+
border-radius: 8px;
74+
border: 1px solid #ccc;
75+
padding: 0.5em;
76+
font-size: 1em;
77+
font-family: inherit;
78+
background-color: #fff;
79+
color: #000;
80+
transition: border-color 0.25s;
81+
margin: 1em;
82+
}
83+
84+
input:focus {
85+
border-color: #646cff;
86+
outline: none;
87+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from "react";
2+
import ReactDOM from "react-dom/client";
3+
import { Compoment, loader } from "./App";
4+
import "./index.css";
5+
import { QueryClientProvider } from "@tanstack/react-query";
6+
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
7+
import { queryClient } from "./queryClient";
8+
import "./axios";
9+
import { RouterProvider, createBrowserRouter } from "react-router-dom";
10+
11+
const router = createBrowserRouter([
12+
{
13+
path: "/",
14+
element: <Compoment />,
15+
loader: loader(queryClient),
16+
},
17+
]);
18+
19+
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
20+
<React.StrictMode>
21+
<QueryClientProvider client={queryClient}>
22+
<RouterProvider router={router} />
23+
<ReactQueryDevtools buttonPosition="bottom-left" />
24+
</QueryClientProvider>
25+
</React.StrictMode>,
26+
);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { QueryClient } from "@tanstack/react-query";
2+
3+
export const queryClient = new QueryClient();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/// <reference types="vite/client" />
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ESNext",
4+
"useDefineForClassFields": true,
5+
"lib": ["DOM", "DOM.Iterable", "ESNext"],
6+
"allowJs": false,
7+
"skipLibCheck": true,
8+
"esModuleInterop": false,
9+
"allowSyntheticDefaultImports": true,
10+
"strict": true,
11+
"forceConsistentCasingInFileNames": true,
12+
"module": "ESNext",
13+
"moduleResolution": "Bundler",
14+
"resolveJsonModule": true,
15+
"isolatedModules": true,
16+
"noEmit": true,
17+
"jsx": "react-jsx"
18+
},
19+
"include": ["src"],
20+
"references": [
21+
{
22+
"path": "./tsconfig.node.json"
23+
}
24+
]
25+
}

0 commit comments

Comments
 (0)