File size: 1,451 Bytes
1e92f2d |
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
'use client'
import * as React from 'react'
import { useActionState } from 'react'
export function FormWithArg<T>({
action,
argument,
id,
children,
}: {
action: (state: string, argument: T) => Promise<string>
argument: T
id: string
children?: React.ReactNode
}) {
const [state, dispatch] = useActionState(
// don't use `bind()`, we want to explicitly avoid getting a FormData argument
// because that always results in a FormData request
(state) => action(state, argument),
'initial-state'
)
return (
<form id={id} action={dispatch}>
<button type="submit">{children}</button>
<span className="form-state">{`${state}`}</span>
</form>
)
}
export function Form({
action,
}: {
action: (state: string, formData: FormData) => Promise<string>
}) {
const [state, dispatch] = useActionState(action, 'initial-state')
return (
<form action={dispatch} id="form-direct">
<button type="submit">Submit server form</button>
<span className="form-state">{`${state}`}</span>
</form>
)
}
export class ErrorBoundary extends React.Component<{
children: React.ReactNode
}> {
state = { error: null }
static getDerivedStateFromError(error) {
return { error }
}
render() {
if (this.state.error) {
return (
<div id="error-boundary">
Error boundary: {this.state.error.message}
</div>
)
}
return this.props.children
}
}
|