Add missing template file App-with-tanstackquery.tsx
01068c8d
1 file(s) · +62 −0
| 1 | + | import { useState } from "react"; |
|
| 2 | + | import beaver from "./assets/beaver.svg"; |
|
| 3 | + | import { useMutation } from "@tanstack/react-query"; |
|
| 4 | + | import type { ApiResponse } from "shared"; |
|
| 5 | + | import "./App.css"; |
|
| 6 | + | ||
| 7 | + | const SERVER_URL = import.meta.env.VITE_SERVER_URL || "http://localhost:3000"; |
|
| 8 | + | ||
| 9 | + | function App() { |
|
| 10 | + | const [data, setData] = useState<ApiResponse | undefined>(); |
|
| 11 | + | ||
| 12 | + | const { mutate: sendRequest } = useMutation({ |
|
| 13 | + | mutationFn: async () => { |
|
| 14 | + | const req = await fetch(`${SERVER_URL}/hello`); |
|
| 15 | + | const res: ApiResponse = await req.json(); |
|
| 16 | + | setData(res); |
|
| 17 | + | }, |
|
| 18 | + | onError: (err) => console.log(err), |
|
| 19 | + | }); |
|
| 20 | + | ||
| 21 | + | return ( |
|
| 22 | + | <> |
|
| 23 | + | <div> |
|
| 24 | + | <a |
|
| 25 | + | href="https://github.com/stevedylandev/bhvr" |
|
| 26 | + | target="_blank" |
|
| 27 | + | rel="noopener" |
|
| 28 | + | > |
|
| 29 | + | <img src={beaver} className="logo" alt="beaver logo" /> |
|
| 30 | + | </a> |
|
| 31 | + | </div> |
|
| 32 | + | <h1>bhvr</h1> |
|
| 33 | + | <h2>Bun + Hono + Vite + React</h2> |
|
| 34 | + | <p>A typesafe fullstack monorepo</p> |
|
| 35 | + | <div className="card"> |
|
| 36 | + | <div className="button-container"> |
|
| 37 | + | <button type="button" onClick={() => sendRequest()}> |
|
| 38 | + | Call API |
|
| 39 | + | </button> |
|
| 40 | + | <a |
|
| 41 | + | className="docs-link" |
|
| 42 | + | target="_blank" |
|
| 43 | + | href="https://bhvr.dev" |
|
| 44 | + | rel="noopener" |
|
| 45 | + | > |
|
| 46 | + | Docs |
|
| 47 | + | </a> |
|
| 48 | + | </div> |
|
| 49 | + | {data && ( |
|
| 50 | + | <pre className="response"> |
|
| 51 | + | <code> |
|
| 52 | + | Message: {data.message} <br /> |
|
| 53 | + | Success: {data.success.toString()} |
|
| 54 | + | </code> |
|
| 55 | + | </pre> |
|
| 56 | + | )} |
|
| 57 | + | </div> |
|
| 58 | + | </> |
|
| 59 | + | ); |
|
| 60 | + | } |
|
| 61 | + | ||
| 62 | + | export default App; |