Next.js Discord

Discord Forum

Error: Functions cannot be passed directly to Client Components unless you explicitly expose ...

Answered
Pyramid ant posted this in #help-forum
Open in Discord
Pyramid antOP
The full error is Error: Functions cannot be passed directly to Client Components unless you explicitly expose it by marking it with "use server".

this is the code
Answered by Ray
try move "use server"; to the top of the file
View full answer

17 Replies

Pyramid antOP
That's barely visible hold on
Now, i am followig the documentaion but im not sure why its throwing the error
Pyramid antOP
type FormDataType = {
  name: string;
  email: string;
  pno?: string;
  message: string;
  subject: string;
};

export async function addData(formData: FormData) {
  "use server";

  const rawFormData = {
    name: formData.get("name"),
  };
  //  formante-contact@formante-contact.iam.gserviceaccount.com
  console.log(rawFormData);
}
i used the "use server" syntax but im pretty confused why it sint working while build
Answer
Pyramid antOP
Let me try
Why am i supposed to do that tho? Can't i just add it inside the function itself?
Oh that seem to work!
@Pyramid ant Why am i supposed to do that tho? Can't i just add it inside the function itself?
when you are defining server action in a seperate file, you have to put 'use server' on top instead
Pyramid antOP
oh right!
also no need to put it inside the function body
Pyramid antOP
right okay! thank you so much
@Pyramid ant Why am i supposed to do that tho? Can't i just add it inside the function itself?
that one is only applicable when you declare the action as a closure inside a server component
fyi
Pyramid antOP
oh ok. i'm pretty new to server actions so im trying to uderstand it even more. thanks for the help!