user5740953
user5740953

Reputation: 179

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 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.

Upvotes: 1

Views: 1704

Answers (1)

Igor Danchenko
Igor Danchenko

Reputation: 3428

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/[email protected]

Upvotes: 4

Related Questions