File size: 1,644 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 |
import * as React from 'react'
import { FormWithArg, Form, ErrorBoundary } from './client'
const action = async (...args: any[]) => {
'use server'
console.log('hello from server', ...args)
return 'state-from-server'
}
// simulate client-side version skew by changing the action ID to something the server won't recognize
setServerActionId(action, 'decafc0ffeebad01')
export default function Page() {
return (
<div>
<div>
<ErrorBoundary>
<Form action={action} />
</ErrorBoundary>
</div>
<div>
<ErrorBoundary>
<FormWithArg
action={action}
id="form-simple-argument"
argument={{ foo: 'bar' }}
>
Submit client form with simple argument
</FormWithArg>
</ErrorBoundary>
</div>
<div>
<ErrorBoundary>
<FormWithArg
action={action}
id="form-complex-argument"
argument={new Map([['foo', Promise.resolve('bar')]])}
>
Submit client form with complex argument
</FormWithArg>
</ErrorBoundary>
</div>
</div>
)
}
function setServerActionId(action: (...args: any[]) => any, id: string) {
// React implementation detail: `registerServerReference(func, id)` sets `func.$$id = id`.
const actionWithMetadata = action as typeof action & { $$id?: string }
if (!actionWithMetadata.$$id) {
throw new Error(
`Expected to find server action metadata properties on ${action}`
)
}
Object.defineProperty(actionWithMetadata, '$$id', {
value: id,
configurable: true,
})
}
|