Proper way to reset an input using server actions
Unanswered
Peruvian anchoveta posted this in #help-forum
Peruvian anchovetaOP
Just as I stated on the title, I have a simple form with one input field. I want to call my server action and upon finishing that action I want to reset the contents of the input field. Wondering what the best way to go about this would be?
16 Replies
Peruvian anchovetaOP
for more context, I did try doing something like
where formAction is coming from "useFormState"
but that throws an error saying that formAction expected 0 paramenters but got one. I believe it still saved everything fine but that error kept coming up on the terminal.
So am wondering if that is not the proper way of doing it or why passing it the formData kept throwing that error?
action={async (formData) => {
formAction(formData);
ref.current?.reset();
});where formAction is coming from "useFormState"
but that throws an error saying that formAction expected 0 paramenters but got one. I believe it still saved everything fine but that error kept coming up on the terminal.
So am wondering if that is not the proper way of doing it or why passing it the formData kept throwing that error?
@Peruvian anchoveta for more context, I did try doing something like
action={async (formData) => {
formAction(formData);
ref.current?.reset();
});
where formAction is coming from "useFormState"
but that throws an error saying that formAction expected 0 paramenters but got one. I believe it still saved everything fine but that error kept coming up on the terminal.
So am wondering if that is not the proper way of doing it or why passing it the formData kept throwing that error?
Turkish Van
Can You provide the
formAction function code?Peruvian anchovetaOP
I went with adding a success on my intial state and returned "ok" when my server action finished without errors and used "useEffect" to check when the state changes and did the ref.current?.reset() when it returns "ok" seems to work fine
I think this works fine and doesnt do anything funky or hacky. My server action just returns something like this >
so basically now I just return ok on success and use the state on my form client componet to reset the form. That sounds better and more proper. what do you think @Turkish Van
American Crow
It's funny when i tested with form server actions i had the same problem but the other way around.
The form always resetted on errors and i wanted to keep the user values.
I think i used uncontrolled (stateless) inputs.
I ended up doing the same thing as you but instead of returning nothing i had to return the submitted data (onError) again
The form always resetted on errors and i wanted to keep the user values.
I think i used uncontrolled (stateless) inputs.
I ended up doing the same thing as you but instead of returning nothing i had to return the submitted data (onError) again
@American Crow It's funny when i tested with form server actions i had the same problem but the other way around.
The form always resetted on errors and i wanted to keep the user values.
I think i used uncontrolled (stateless) inputs.
I ended up doing the same thing as you but instead of returning nothing i had to return the submitted data (onError) again
Peruvian anchovetaOP
yeah, its weird. the first approach just gives a type error but saves fine? which is odd. so thats a no go. and the second method with useEffect works fine. So I think im sticking to it. Just wondering what other people do and whats the proper way of doing so if any.
American Crow
yea both feels a bit wrong but not too wrong to get into it, at least to me 😄
maybe others know a better approach just leave the therad open
maybe others know a better approach just leave the therad open
Peruvian anchovetaOP
yeah, dont know if theres a more "official" way of doing so. lets see if anyone else has an opinion on it
@Peruvian anchoveta so basically now I just return ok on success and use the state on my form client componet to reset the form. That sounds better and more proper. what do you think <@439879724412567553>
Turkish Van
So, if I understood it properly, You want to reset the form inputs in case the Server Action got executed successfully, but keep the input fields filled (not reset the form) in case something went wrong.
What if You, in case of an
What if You, in case of an
Error, made a Server Action throw an error and handled it in a way like this:<form action={()=>{
formAction().then(()=>ref.current?.reset()).catch(()=>{console.log('Error')})
}}
ref={ref}>
{/* Form Content */}
</form>@Turkish Van So, if I understood it properly, You want to reset the form inputs in case the Server Action got executed successfully, but keep the input fields filled (not reset the form) in case something went wrong.
What if You, in case of an `Error`, made a Server Action throw an error and handled it in a way like this:
ts
<form action={()=>{
formAction().then(()=>ref.current?.reset()).catch(()=>{console.log('Error')})
}}
ref={ref}>
{/* Form Content */}
</form>
American Crow
That won't work server actions swallow errors in prod. Don't throw errors within server actions
https://joulev.dev/blogs/throwing-expected-errors-in-react-server-actions
https://joulev.dev/blogs/throwing-expected-errors-in-react-server-actions
@American Crow That won't work server actions swallow errors in prod. Don't throw errors within server actions
https://joulev.dev/blogs/throwing-expected-errors-in-react-server-actions
Turkish Van
Yes, You are right. Thanks!
You wouldn't get an actual
You could also follow the same approach and make Your Sever Action return response, for example, in the following form:
You wouldn't get an actual
Error message, but an Error would still occur so if You don't want to handle an Error based on it's content, it could still work for You.You could also follow the same approach and make Your Sever Action return response, for example, in the following form:
{data: {}, error: {}}. Following that it could check if an Error occurred or not: formAction().then((res)=>{res.error == null && ref.current?.reset()})@Peruvian anchoveta I think this works fine and doesnt do anything funky or hacky. My server action just returns something like this >
Turkish Van
It does not have to be the format I used above, You could apply the same thing to Your response format. Just make sure there is something inside of that response that could be used to check if an
Error occurred.@Turkish Van It does not have to be the format I used above, You could apply the same thing to Your response format. Just make sure there is something inside of that response that could be used to check if an `Error` occurred.
American Crow
I think the OP already knows this. He is already returning
It's more an open question on best practices. At least thats how i understand it
zoderrorsand success in his response.It's more an open question on best practices. At least thats how i understand it
Peruvian anchovetaOP
sorry had to step out, yeah I already got a success and error property. Its working properly now weather theres a error or it succeeds. Like @American Crow said, its more of a "is this standard protocol to handling it" than anything else. Thank you for your input tho, its good to know how others would do this @Turkish Van