Created
April 7, 2024 22:56
-
-
Save yusukebe/fdeef5b8b315d930ef75c832405cf0a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// app/routes/echo/_title.island.tsx | |
import { useEffect, useState } from 'hono/jsx' | |
import { hc } from 'hono/client' | |
import type { Hono } from 'hono' | |
import api from './api' | |
export const apiClient = <T extends Hono>(path: string = './api') => { | |
let url: string | undefined | |
if (globalThis.window) { | |
url = new URL(path, window.location.href + '/').toString() | |
} else { | |
url = path | |
} | |
return hc<T>(url) | |
} | |
export default function Title() { | |
const [title, setTitle] = useState('') | |
useEffect(() => { | |
const client = apiClient<typeof api>('./api') | |
;(async () => { | |
const res = await client.echo.$post({ | |
json: { | |
message: 'Hello' | |
} | |
}) | |
const data = await res.json() | |
setTitle(data.message) | |
})() | |
}, []) | |
return <h1>{title}</h1> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment