submitting form results in page reload/url change t3+react-hook-form+shadcn
I'm trying to build a form using react hook form and shadcn form component, but it keeps reloading the page after submit and add the "?username=" to the url (which makes it ignore the validation since it just redirects the url), I copy pasted the same code as shadcn has on his website for testing, still does it, cant figure out the issue.
anyone knows how to fix it?
Solution:Jump to solution
is there a reason you have
void form.handleSubmit
? pretty sure that should just be form.handleSubmit
33 Replies
You’d have to show code
Of what tho? I would just be copy pasting the example from his website which I linked
it just pushes me to /dashboard?username=
and reloads
What happens if you add an event parameter
e
to your onSubmit
function, then call e.preventDefault()
as the first line of the function?
Function signature for onSubmit
can be found here: https://react-hook-form.com/docs/useform/handlesubmitlike this?
I tried it before and it gives me an error
So it does expect an optional event param, but the type defs don't match? What happens if you type
e
as any
, just to try it?doesnt give me the error there, but gives me a different error
Shouldn’t need to do this at all
You sure your form isn’t inside another form and the button is propagating?
I have the form component called in Dashboard which is a nextjs page
Weird
I'll defer to barry since I've only used react-hook-form a little, but just to close the loop: I think you're using the wrong
Event
type in your onSubmit
function definition. If you want to follow this up, I would track down the Event
type that's expected by react-hook-form and make sure I'm importing that and not something else.And I am on the phone and the only way I find myself debugging this is running it locally
Try just making a new project again see if it persists
yep, made a new project and it seems like it persists
website is being refrehsed and ?username= is added to the url
The fuck
You using some random browser?
💀
Ive used react form hook with next js (without t3 /shadcn) and jt worked flawlessly
If you are what about chrome does it happen there
I’ll say I saw this as well when I had a form in a dialog not nested in a form on the main page. Submitting one submitted the other for some reason. Prevent default seemed to work for me
Im on chrome 😭
Must be something with t3
I dont know what else can cause this
I made this with shad form yesterday try cloning and see if it happens in my app too
GitHub
GitHub - barrybtw/aktiesparekonto
Contribute to barrybtw/aktiesparekonto development by creating an account on GitHub.
I will have to do it tomorrow, already 2 am and i gotta wake up early
I will keep you updated when I check
Solution
is there a reason you have
void form.handleSubmit
? pretty sure that should just be form.handleSubmit
also - do yourself a favour and wrap your
onSubmit
in handleSubmit
in the same place you declare it. that way you don't have to do any manual types for the function:
on this, i think handleSubmit
injects a preventDefault
call for you so that this exact reload behaviour doesn't happen
but you're putting void
beforehand, which evaluates the handleSubmit
call but then returns undefined
, rather than the result of handleSubmit
so really it's just not doing anythingyeah, I do it to avoid this error, I used this trick before and it never did a problem to me
will do
that's not an error, its eslint yelling at you and in this case you should ignore it
okay, I did it like this and removed the void but it still reloads and changes the url
wait I put it in the wrong component
it did work
wow
damn enlist
I’ve done that before no problem. Weird it’s bugging here
I don't see how that'd work haha, no
onSubmit
handler would end up being definedBecause it’s a type
Telling typescript it returns void
I’ve never had that problem of it doing undefined
void
is a JS operator, not a type
well
it is a type
but not in this context💀 I've seen people use it with t3 for ages, something must be amiss
yeah i've seen ppl use it too but not there haha