Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] [v7] Support binding additional arguments #29

Closed
varna opened this issue Oct 18, 2023 · 3 comments · Fixed by #97
Closed

[FEATURE] [v7] Support binding additional arguments #29

varna opened this issue Oct 18, 2023 · 3 comments · Fixed by #97
Labels
enhancement New feature or request
Milestone

Comments

@varna
Copy link

varna commented Oct 18, 2023

Problem

If you have a CRUD like model, you might want to provide two arguments instead of one (schema, and id of item being updated/removed). Personally, at the moment I'm doing something like this:

// /app/items/[id]/actions.ts

export const updateItem = action(z.tuple([schema, idSchema]), async (args, { updatedById }) => {
  const [data, id] = args
 
  return ItemModel.update({
    where: { id },
    data: {
      ...data,
      updatedById
    }
  })
})

Today I found out that you can bind additional arguments to actions like this:

Next.js solution

https://nextjs.org/docs/app/api-reference/functions/server-actions#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.

// app/client-component.jsx
'use client'
 
import { updateUser } from './actions'
 
export function UserProfile({ userId }) {
  const updateUserWithId = updateUser.bind(null, userId)
 
  return (
    <form action={updateUserWithId}>
      <input type="text" name="name" />
      <button type="submit">Update User Name</button>
    </form>
  )
}

And then, the updateUser Server Action will always receive the userId argument, in addition to the form data:

// app/actions.js
'use server'
 
export async function updateUser(userId, formData) {
  // ...
}

Good to know: .bind of a Server Action works in both Server and Client Components. It also supports Progressive Enhancement.

next-safe-action

I wanted to ask others, what is their preferred way to do this.

@tiavina-mika
Copy link

is there any update on this?

@TheEdoRan TheEdoRan changed the title Actions can have multiple arguments via .bind(null, ...args) syntax [FEAT] Support Form Actions Feb 7, 2024
@TheEdoRan TheEdoRan added the enhancement New feature or request label Feb 7, 2024
@TheEdoRan
Copy link
Owner

I'm actively working on v7 release in the next branch, which will support, among other features, Form Actions too. We just have to find the right implementation for them. I'll update this issue with relevant thoughts and changes.

@TheEdoRan TheEdoRan changed the title [FEAT] Support Form Actions [FEATURE] Support Form Actions in v7 Feb 7, 2024
@TheEdoRan TheEdoRan changed the title [FEATURE] Support Form Actions in v7 [FEATURE] [v7] Support Form Actions Feb 7, 2024
@TheEdoRan TheEdoRan added this to the v7 milestone Feb 8, 2024
@benjick
Copy link

benjick commented Feb 18, 2024

I include the id using hidden inputs:

<input type="hidden" value="id" value={post.id} />

@TheEdoRan TheEdoRan changed the title [FEATURE] [v7] Support Form Actions [FEATURE] [v7] Support additional arguments (bind, hidden inputs) Apr 6, 2024
@TheEdoRan TheEdoRan changed the title [FEATURE] [v7] Support additional arguments (bind, hidden inputs) [FEATURE] [v7] Support binding additional arguments Apr 6, 2024
@TheEdoRan TheEdoRan linked a pull request Apr 8, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants