Next.js Discord

Discord Forum

Handling Client-Side and Server-Side Logic in Nextjs 14

Unanswered
West African Crocodile posted this in #help-forum
Open in Discord
West African CrocodileOP
I followed the new next14 course and have a question about chapter 12 mutating data: https://nextjs.org/learn/dashboard-app/mutating-data . In this chapter you import a deleteInvoice function from lib/actions.ts which is marked "use server" then assign it to the delete button in a component as follows:

import { deleteInvoice } from "@/app/lib/actions";

export function DeleteInvoice({ id }: { id: string }) {
const deleteInvoiceWithId = deleteInvoice.bind(null, id);
return (
<form action={deleteInvoiceWithId}>
'......

This works, but I tried to better understand it and never used .bind so tried to do it a different way:

import { deleteInvoice } from "@/app/lib/actions";

export function DeleteInvoice({ id }: { id: string }) {
const deleteInvoiceWithId = () => { return deleteInvoice(id); };
return (
<form action={deleteInvoiceWithId}>
'......

However this gets me the error: Unhandled Runtime Error

Error: Functions cannot be passed directly to Client Components unless you explicitly expose it by marking it with "use server". <form action={function} children=...> ^^^^^^^^^^

could someone help explain this? Don't both methods create a new function which is still called on the server? And how would "use server" expose it (as described in the error)? I tried reading the docs but the whole server side/ client side abstraction doesn't really click in my head, especially with this example. Thanks for your help!

5 Replies

Cape lion
Next docs say: Binding Arguments
You can bind arguments to a Server Action using the bind method. This allows you to create a new Server Action with some arguments already bound. This is beneficial when you want to pass extra arguments to a Server Action.
Shy Albatross
binding isn't free, it means the payload isn't encrypted like it is with regular server action call, so you're potentially exposing stuff
"use server" creates an endpoint and a POST request fetch pair to facilitate crossing the client-server boundary
that error tells you that there is no way for the client to call it unless you expose it explicitly