Skip to content

Commit f908da7

Browse files
Merge pull request #3 from codenickycode/create-hono-example-on-client
create hono example client-side
2 parents 7254d35 + f4c8dc2 commit f908da7

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

client/src/example.tsx

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,42 @@ import { Spinner } from './components/ui.spinner';
33
import { Modal } from './components/ui.modal';
44
import { useQuery } from '@tanstack/react-query';
55
import { getServerUrl } from './config';
6+
import { hc } from 'hono/client';
7+
import type { ServerApi } from '../../server/src/types';
8+
import { useMonitor } from './services/monitor.use-monitor';
69

710
const serverUrl = getServerUrl();
811

9-
export const Example = () => {
10-
const [isModalOpen, setIsModalOpen] = useState(false);
11-
const serverOkQuery = useQuery({
12-
queryKey: ['example'],
12+
const { $get } = hc<ServerApi>(serverUrl)['index'];
13+
14+
const useServerOkQuery = () => {
15+
const { captureException } = useMonitor();
16+
return useQuery({
17+
queryKey: ['hello-from-server'],
1318
queryFn: async () => {
14-
return fetch(serverUrl)
19+
return $get()
1520
.then(async (res) => {
1621
if (!res.ok) {
1722
throw res;
1823
}
19-
return res.text();
24+
return res.json();
2025
})
2126
.catch((error) => {
22-
console.error(error);
27+
captureException(error);
2328
return null;
2429
});
2530
},
2631
});
32+
};
33+
34+
export const Example = () => {
35+
const [isModalOpen, setIsModalOpen] = useState(false);
36+
const serverOkQuery = useServerOkQuery();
2737
return (
2838
<div className="flex flex-col gap-4">
2939
<h1>Hello from React!</h1>
3040
<div>
31-
<p>Server connection: {serverOkQuery.data ? '✅' : '❌'}</p>
41+
<p>Server connection: {serverOkQuery.isSuccess ? '✅' : '❌'}</p>
3242
</div>
3343
<div>
3444
<p>

client/tsconfig.app.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
"jsx": "react-jsx",
1313
"types": ["vitest/globals"]
1414
},
15-
"include": ["src"]
15+
"include": ["src", "../server/src/*"]
1616
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "create-react-spa-cloudflare",
33
"type": "module",
4-
"version": "0.0.18",
4+
"version": "0.0.19",
55
"description": "Starter package for react spa with cloudflare pages, workers, and kv",
66
"bin": "./bin/cli.js",
77
"scripts": {

0 commit comments

Comments
 (0)