Submission hack to prevent page reloading #3724
Unanswered
axelrindle
asked this question in
Show and tell
Replies: 1 comment
-
submit(e) { |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The Issue ⁉
I stumbled across a weird phenomenon today. Even though a reload of the browser page should be prevented, my browser (MS Edge with the Chromium Engine) still did a reload on form submission. Despite added a manual
e.preventDefault()
(such as suggested in #3235) to it my browser still reloaded.To work around this issue, I came up with a small hack to handle the form submission manually.
Didn't test any other browser though, because why take the easy way when it can be difficult?
Code Examples 💻
Form.tsx
MyPage.tsx
Calling
form.current?.submit()
effectively callsformik.current?.handleSubmit()
as defined insideForm.tsx
.Conclusion 🎀
While the example code is specific to the usage of
@tanstack/query
an adaption to other fetch methods should be fairly easy.Some may say this is a misusage of React Ref's, but it works this way. So who cares?
For a real life usage you can view the source code of an application I'm currently working on (this commit might be of interest too).
Let me know what you think and if you've found any bugs 🐛
Advertising not intended.
Beta Was this translation helpful? Give feedback.
All reactions