admin管理员组文章数量:1279042
I want to display a toast for a few seconds whenever a user does something, eg. when they log in to the app. I am using a form on /login/+page.svelte
to login, with the database interaction in /login/page.server.js
. And I am using a writable store
to store toasts.
On form submit, the page refreshes, so my store is cleared and the toast is lost. It seems the event flow is:
- submit form to
/login/page.server.js
page.server.js
does some stuffpage.server.js
sends back the full page and the browser reloads to the new full page.
I understand you can use preventdefault
to prevent all those steps, but I only want to prevent the reloading. Preventing everything does not seem optimal (there are probably some other stuff I don't even know I'm preventing).
Is there a nicer way of interacting between a page.svelte
and a page.server.js
without reload (and thus clearing, probably all, stores) than preventdefault + using a manual fetch?
REPL I was playing around with that demonstrates the toast staying full 3 seconds generally, but immediately disappearing on normal form submit. /repl/8b61434332ca471b83cbf039bf1f3fc9?version=3.22.0
I want to display a toast for a few seconds whenever a user does something, eg. when they log in to the app. I am using a form on /login/+page.svelte
to login, with the database interaction in /login/page.server.js
. And I am using a writable store
to store toasts.
On form submit, the page refreshes, so my store is cleared and the toast is lost. It seems the event flow is:
- submit form to
/login/page.server.js
page.server.js
does some stuffpage.server.js
sends back the full page and the browser reloads to the new full page.
I understand you can use preventdefault
to prevent all those steps, but I only want to prevent the reloading. Preventing everything does not seem optimal (there are probably some other stuff I don't even know I'm preventing).
Is there a nicer way of interacting between a page.svelte
and a page.server.js
without reload (and thus clearing, probably all, stores) than preventdefault + using a manual fetch?
REPL I was playing around with that demonstrates the toast staying full 3 seconds generally, but immediately disappearing on normal form submit. https://svelte.dev/repl/8b61434332ca471b83cbf039bf1f3fc9?version=3.22.0
Share Improve this question asked Nov 10, 2022 at 14:42 JoramJoram 3045 silver badges15 bronze badges1 Answer
Reset to default 10The intended workflow for forms is to use form actions and enhance
, which automatically processes the form asynchronously (which falls back to the hard reload if JS is disabled).
本文标签:
版权声明:本文标题:javascript - Can you stop a page from reloading on form submit without preventDefault with svelte kit? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741281042a2370007.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论