Next.js Discord

Discord Forum

Server actions should support statically typed conversions for you between server <-> client

Unanswered
Orinoco Crocodile posted this in #help-forum
Open in Discord
Orinoco CrocodileOP
When creating a server action i want to use fully statically typed features
`use server`
export async function myServerAction(request: TReq) : Promise<TRes> { ... }


`use client`
// boilerplate
const Component = () => {
  useEffect(() => {
    async submit() { 
      const req = new TReq(...);
      const result = await myServerAction(req);
      // do something
    }
  }, []);
}


but instead I get Error: Only plain objects, and a few built-ins, can be passed to Client Components from Server Components. Classes or null prototypes are not supported.

I understand that there might be limitations to this, but why next.js cannot do JSON.stringify() and JSON.parse() for me in the background? Or why cannot I write a custom serializer for the data somewhere and inject this serializer into the method?

1 Reply

Orinoco CrocodileOP