It is not allowed to define inline "use server" annotated Server Actions in Client Components.
Unanswered
Carpenter wasp posted this in #help-forum
Carpenter waspOP
Error:
× It is not allowed to define inline "use server" annotated Server Actions in Client Components.
│ To use Server Actions in a Client Component, you can either export them from a separate file with "use server" at the top, or pass them down through props from a Server Component.https://nextjs.org/blog/next-14#server-actions-stable
'use client'
import { useUser } from "@/app/_context/UserContext";
import Link from "next/link";
export default function Dashboard() {
async function test(formData: FormData) {
'use server';
console.log("show this to server");
}
return (
<main>
<h1>Dashboard Page</h1>
<Link className="btn btn-primary" href={"/dashboard/test2"}>
Next
</Link>
<Link className="btn btn-primary" href={"/auth/"}>
Auth
</Link>
</main>
);
}how to use?
6 Replies
@Carpenter wasp
Error:
× It is not allowed to define inline "use server" annotated Server Actions in Client Components.
│ To use Server Actions in a Client Component, you can either export them from a separate file with "use server" at the top, or pass them down through props from a Server Component.
https://nextjs.org/blog/next-14#server-actions-stable
'use client'
import { useUser } from "@/app/_context/UserContext";
import Link from "next/link";
export default function Dashboard() {
async function test(formData: FormData) {
'use server';
console.log("show this to server");
}
return (
<main>
<h1>Dashboard Page</h1>
<Link className="btn btn-primary" href={"/dashboard/test2"}>
Next
</Link>
<Link className="btn btn-primary" href={"/auth/"}>
Auth
</Link>
</main>
);
}
how to use?
You can only declare inline server actions like this in a server component. Not a client component. Your Dashboard is marked with use client hence is a client component.
@joulev You can only declare inline server actions like this in a server component. Not a client component. Your Dashboard is marked with use client hence is a client component.
Carpenter waspOP
why we still use inline server actions in server component? thats weird
Carpenter waspOP
i think we can use inline server action in client components
but why we need inline server actions in server component????? next js get more weird this days
@Carpenter wasp i think we can use inline server action in client components
no you can't. server actions run on the server, so it needs to import server-side things, which means it cannot be declared in a client component which will run on the client
@Carpenter wasp why we still use inline server actions in server component? thats weird
i don't. i make a new file and add
BUT there are people who do, and react does this to support those people.
"use server" at the top. i don't use the inline server action syntax.BUT there are people who do, and react does this to support those people.