Skip to content

Instantly share code, notes, and snippets.

@yusukebe
Last active June 25, 2024 04:47
Show Gist options
  • Save yusukebe/817a8b707bb338940bccd8a83231a96c to your computer and use it in GitHub Desktop.
Save yusukebe/817a8b707bb338940bccd8a83231a96c to your computer and use it in GitHub Desktop.
import { Hono } from 'hono'
import { createAction } from './types'
const app = new Hono()
const [action, Component] = createAction(app, (data) => {
if (data) {
return <p>Your name is {data.name}!</p>
}
return <p>Input your name</p>
})
app.get('/', (c) => {
return c.render(
<>
<form action={action}>
<input type="text" name="name" placeholder="Your Name" />
<button type="submit">Send</button>
</form>
<Component />
</>
)
})
export default app
import { Hono } from 'hono'
import { createAction } from './types'
const app = new Hono()
const [action] = createAction(app, (data) => {
console.log(`Your name is ${data.name}!`)
})
app.get('/', (c) => {
return c.render(
<form action={action}>
<input type="text" name="name" placeholder="Your Name" />
<button type="submit">Send</button>
</form>
)
})
export default app
import { Hono } from 'hono'
import { createAction } from './types'
type Env = {
Variables: {
todos: any
}
}
const app = new Hono<Env>()
const [action, Component] = createAction(app, async (data, c) => {
if (data) {
await c.var.todos.add(data)
}
const todos = await c.var.todos.getAll()
return (
<ul>
{todos.map((todo) => {
return (
<li>
{todo.title}
{/** deleteAction is not defined yet. Reference error?? */}
<form action={deleteAction}>
<button type="submit">Delete</button>
<input type="hidden" name="id" value={todo.id} />
</form>
</li>
)
})}
</ul>
)
})
const [deleteAction] = createAction(app, async (data, c) => {
await c.var.db.delete(data.id)
action()
})
app.get('/', async (c) => {
return c.render(
<>
<form action={action}>
<input type="text" name="title" placeholder="Title" />
<button type="submit">Add</button>
</form>
<Component />
</>
)
})
export default app
import { Hono } from 'hono'
import { createAction } from './types'
type Env = {
Variables: {
todos: any
}
}
const app = new Hono<Env>()
const [action, Component] = createAction(app, async (data, c) => {
const [deleteAction] = createAction(app, async (data, c) => {
await c.var.db.delete(data.id)
action()
})
if (data) {
await c.var.todos.add(data)
}
const todos = await c.var.todos.getAll()
return (
<ul>
{todos.map((todo) => {
return (
<li>
{todo.title}
<form action={deleteAction}>
<button type="submit">Delete</button>
<input type="hidden" name="id" value={todo.id} />
</form>
</li>
)
})}
</ul>
)
})
app.get('/', async (c) => {
return c.render(
<>
<form action={action}>
<input type="text" name="title" placeholder="Title" />
<button type="submit">Add</button>
</form>
<Component />
</>
)
})
export default app
import type { Hono, Context } from 'hono'
interface ActionHandler {
(data: Record<string, any>, c: Context): any
}
type ActionReturn = [any, any]
export declare function createAction(app: Hono, handler: ActionHandler): ActionReturn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment