Next.js Discord

Discord Forum

Ability to pass Self-Contained callback functions from server components to client components

Unanswered
Brown bear posted this in #help-forum
Open in Discord
Brown bearOP
So I get why you can't pass functions that are not server actions to client components.
But what about functions that don't have closures? Like entirely self contained callbacks.
For example a component that accepts a valueFormatter function. The function should run on the client, and use no outside scope except for what is available from inside the function scope.
The workaround is to define a client component that accepts the same props as the client component, but has the valueFormatter function already filled in. For example (value)=>value.toString() to the client.
This is a lot of effort just to provide that 1 line function to the client. Another good example of this is MUI's sx callback functions that pass the theme so you can do advanced stuff with it.

You can't serialize functions, but that function doesn't really need to be serialized. Just included in the client bundle.

1 Reply

Brown bearOP
Sorry, I guess the question I am asking here is if there is any other better way to do this, without having to split my component into a seperate file just to use a 1 line function