Skip to content

Instantly share code, notes, and snippets.

@yusukebe
Created April 7, 2024 22:56
Show Gist options
  • Save yusukebe/fdeef5b8b315d930ef75c832405cf0a1 to your computer and use it in GitHub Desktop.
Save yusukebe/fdeef5b8b315d930ef75c832405cf0a1 to your computer and use it in GitHub Desktop.
// 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