-
Notifications
You must be signed in to change notification settings - Fork 26
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
expose the input in useServerAction
#155
base: main
Are you sure you want to change the base?
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
🦋 Changeset detectedLatest commit: 7d5f7c8 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
@@ -144,6 +144,7 @@ function MyComponent() { | |||
- `execute`: A function to execute the server action with the provided input. | |||
- `setOptimistic`: A function to set an optimistic update for the `data` value. It accepts either the new data directly or a function that receives the current data and returns the new data. | |||
- `reset`: A function to reset the state of the server action to its initial values. | |||
- `lastUsedInput`: The last input evaluated by the server action to get to the current state. This value will be the input before parsing and validation. If the input is FormData, it will be represented as a JSON object. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I'm understanding correctly this solution has the same problem as described here no?
Why returning data when error is not a good idea? I would like to understand.
Traditional server techs like Rails or others validate a form and you can access old data in the fields that didn't fail.
And the important part is you always access data, in the same way, it doesn't matter if there was an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question. I think we have a different definition of data
. The way I define it, and the way its represented in zsa
docs, is that it is the return value of a successful handler. What you returned in your PR is not data, rather it is what I define as input
. Input is the value passed to the server action.
data
: the successful return value of a server action's handlerinput
: the value sent to the server action over the networkerror
: a thrown error in a server action
So for example, if we have a simple action
export const simpleAction = createServerAction()
.input(
z.object({
name: z.string(),
})
)
.handler(async ({ input }) => {
return `Hello, ${input.name}`
})
Applied definitions:
data
:Hello, ${name}
input
: { name: "..." }error
: a thrown error in a server action
ZSA server actions must follow the following type:
(input) => [data, null] | [null, err]
I think we are getting at the same thing, but what you are looking for is what I define as input
and you define as data
.
Rather than doing const { data } = useServerAction(...)
, you will do const { input } = useServerAction(...)
. From the open source code you posted, I see your function doesn't have data
because it is doing a redirect.
Hopefully that makes sense!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It makes totally sense, thanks for the detailed response!
looking forward to try this one. ping me when you have ready and I'll be the first Beta tester :)
@andresgutgon all tests are now passing here if you want to Beta test 🙏 |
I won't work on this until the weekend probably. Feel free to merge and I'll rebase my fork and try Thanks! |
Hi @IdoPesok sorry for the delay 🙏 |
@IdoPesok anything missing here? I would love to have this 🙏 |
adds ability for hook to store input raw as mentioned in #124