英文:
Server Actions in next.js
问题
I am using server actions in next.js as per the documentation [Server Action] https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions
My action code is
"use server"
export async function registerAndLoginUser(data: any) {
console.log(JSON.stringify(data));
}
And the action is used in a client side component
<form
action={async (data: any) => {
try {
await registerAndLoginUser(data);
} catch (e: unknown) {
console.error(e);
}
}}
>;
I get an error:
Type '(data: any) => Promise<void>' is not assignable to type 'string'.ts(2322)
index.d.ts(2143, 9): The expected type comes from property 'action' which is declared here on type 'DetailedHTMLProps<FormHTMLAttributes<HTMLFormElement>, HTMLFormElement>'.
In short, the action property only accepts a string value.
英文:
I am using server actions in next.js as per the documentation [Server Action] <https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions>
My action code is
"use server"
export async function registerAndLoginUser(data: any) {
console.log(JSON.stringify(data));
}
And the action is used in a client side component
<form
action={async (data: any) => {
try {
await registerAndLoginUser(data);
} catch (e: unknown) {
console.error(e);
}
}}
>
i get error
Type '(data: any) => Promise<void>' is not assignable to type 'string'.ts(2322)
index.d.ts(2143, 9): The expected type comes from property 'action' which is declared here on type 'DetailedHTMLProps<FormHTMLAttributes<HTMLFormElement>, HTMLFormElement>'
In short action property only accepts string value.
答案1
得分: 2
你可以通过将你的 @types/react
包升级到最新版本来解决这个 TypeScript 错误。在 @types/react@18.0.38
版本中添加了对异步表单操作的实验性支持。
英文:
You can fix this TypeScript error by upgrading your @types/react
package to the latest version. Experimental support for async form actions was added in @types/react@18.0.38
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论