/
TrpcDemo.tsx
39 lines (27 loc) · 905 Bytes
/
TrpcDemo.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/** @jsx h */
import { h } from "preact";
import { useState } from "preact/hooks";
import { Button } from "../components/Button.tsx";
import { trpc } from "../utils/trpc.ts";
export default function Counter() {
const [helloResponse, setHelloResponse] = useState('');
const [posts, setPosts] = useState<{name: string}[]>([]);
const fetchPosts = () => trpc.query("post.get").then(setPosts)
return (
<div>
<Button onClick={() => {
trpc.query("hello").then(setHelloResponse)
}}>Hello</Button>
{helloResponse}
<hr />
<Button onClick={() => {
trpc.mutation("post.create", {name: `Random post ${Math.random()}`}).then(fetchPosts)
}}>Create Random Post</Button>
<hr />
<Button onClick={fetchPosts}>Get Posts</Button>
<ul>
{posts.map((post, i) => <li key={i}>{post.name}</li>)}
</ul>
</div>
);
}