Next.js Discord

Discord Forum

Currying server actions

Answered
American Crocodile posted this in #help-forum
Open in Discord
American CrocodileOP
Is it possible to Curry server actions.

I have server component that gets an id as param. I define a server action in that component that I need to pass to a Client Component. I want the server action to be curried with the id.

Will I just have to pass the id down to the Client Component, I do not like that because it is multi use
Answered by American Crocodile
Ok, realized I was using more than the id from the parent component, fixed that and now this works.
View full answer

3 Replies

American CrocodileOP
export async function SendFileModal({params: {id}) {

function sendFile(formData) {
'use server'
// do something
api.sendFile(id, file) // this doesnt work probably because id is out of context
}
return <ClientComponent action={sendFile}
 />
}
export async function SendFileModal({params: {id}) {

function getSendFile(theId) {
return function sendFile(formData) {
'use server'
// do something
api.sendFile(theId, file) // this doesnt work probably because id is out of context
}}
return <ClientComponent action={getSendFile(id)} /> // Was hoping this would work, but same error
}
American CrocodileOP
Ok, realized I was using more than the id from the parent component, fixed that and now this works.
Answer