Next.js Discord

Discord Forum

UseActionState type error

Unanswered
ice cream posted this in #help-forum
Open in Discord
Avatar
heyoo, Where am I using the useaction state wrong? I am returning a promise from an async function on the action state but it's giving me a type error. I use nextjs 15 app router and here is the relevant code :
  // page.tsx it has a use client on top
  const [state, formAction] = useActionState(registerUser, registerInitialState); // there is a type error here

  // actions.tsx it has a use server on top
  export const registerUser = async (prevState :any, props  : FormData) => {
      // uses zod.safeparse to validate the data
     if(!validatedData.success){
         return {
             success: false,
             error: validatedData.error.errors 
          }
      }
      const insert = await db.insert(users).values({...data, id})
      return {
          success: true,
          data: data
      }   
  }

2 Replies

Avatar
Asian black bear
If you want help you should post the actual error you are seeing.
Avatar
oh, srry about that, here's my error on TS :
No overload matches this call. Overload 1 of 2, '(action: (state: string) => string | Promise<string>, initialState: string, permalink?: string | undefined): [state: string, dispatch: () => void, isPending: boolean]', gave the following error. Argument of type '(prev: any, data: FormData) => Promise<string>' is not assignable to parameter of type '(state: string) => string | Promise<string>'. Target signature provides too few arguments. Expected 2 or more, but got 1. Overload 2 of 2, '(action: (state: string, payload: FormData) => string | Promise<string>, initialState: string, permalink?: string | undefined): [state: string, dispatch: (payload: FormData) => void, isPending: boolean]', gave the following error. Argument of type '{ email: string; password: string; }' is not assignable to parameter of type 'string'.ts(2769)