Next.js Discord

Discord Forum

Custom types with actions

Unanswered
Cape lion posted this in #help-forum
Open in Discord
Cape lionOP
If you try to pass a parameter to an action that isn't a primitive you currently receive an error. Is there any way to register codecs for other types?

49 Replies

Cape lionOP
Given you have a server action

// Server Component
export default function Page() {
  // Server Action
  async function add(amount : BigInt, formData: FormData): BigInt {
    'use server'
    return updateCount((count) => count + amount )
  }
}


you'll receive an error because server actions only support primitives as parameters. If I were to want to use a non primitive type like BigInt, is it possible to register a codec with nextjs to encode/decode the BigInt?
Server actions have to be serializable.
In nexttjs there is no way that I know of to do what your asking. Just stringify it and convert it back on the server action side.
Cape lionOP
That's my question, is it possible to register custom serialisers (codecs)
ah ok
Negative, and I beleive thats a react limitation, not nextjs.
Cape lionOP
So I need to provide my own layer for that
Yes sir.
Cape lionOP
ok cool, thanks for the clarification
disclaimer, this is as far as I know........ I havent had to work with this specific situation
Cape lionOP
ok, let's see if anyone else chimes in then, admittedly I haven't been able to find anything saying otherwise in my searches
If your googling... I would go to more react route and look up documentation on server actions in react
Nextjs doesnt do much in the way of extending the server actions from base react.
Cape lionOP
right
it actually says bigint is supported
BigInt is a primative, sorry didnt think about that.
Cape lionOP
ha, what do you know
I'm still surprised
So were you getting an error? Or just assuming it wouldnt work?
Cape lionOP
It's pretty clear that you're stuck with the list though
I tried it with another datatype (luxon DateTime) and got the error
Ahhh, yeah. That makes more sense!
Cape lionOP
I assumed it would be the same with bigint but at least that one works
use epoch time 😄
err unix time, whatever you wanna call it
Cape lionOP
yeah, maybe that is the way to go
it's a slightly unfortunate constraint on the system though, having to use a limited set of datatypes in the calls to actions
Just remember, all a server action is... its just a post request, so its going to be serialized over the wire.
Cape lionOP
sure, but with a traditional api you have the opportunity to control serialisation
For sure, I think server actions are pretty new still, im sure we will eventually get the ability to control them a bit more.
Cape lionOP
it does seem like a pretty natural requirement
Yeah, I forsee in the near future having the ability to use server actions for fetches and not just mutations as well, in a non-asynchronous fashion. To me, the asyncronous nature of server actions seems like a silly and artificial limitation considering.
Cape lionOP
We already organise our codebase with fetches alongside mutations
and well I know its artificial to a degree as react specifically states its up to the framework.
Cape lionOP
I haven't explored the line between what react and next.js take responsibility for here
AFAIK, server actions are very un-tainted by nextjs.
I know frameworks have a bit of control, this is where I say they are artificially made to occur one at a time, wonder why the limitation
Cape lionOP
That does seem a strange constraint, curious where that comes from
It kind of makes sense for mutations, I just dont understand why say its only for mutations.
To me it seems like a half baked feature they intend on expanding on.
Cape lionOP
It makes sense if that actions are targetted the same state, but aside from that I don't see why they would need to be processed in series.
yeah, early days
use server in no ways indicates that its for mutations only, makes more sense for that to be the catalyst to alot more possibilities
but anyways, I think that mostly covers the question hahah. Ima get back to work. GL 🙂
Cape lionOP
yes that's very clear now, many thanks
Make sure to mark an answer to the post when you get a min @Cape lion