I need helps with @tanstack/svelte-form. How should I implement confirmation dialog for a dirty form
I need
window.confirm
to show when leaving the form with unsaved changes. The picture my is the code snippet, can you check that something was wrong?
18 Replies
rare-sapphire•5w ago
what is the value of
isDirty
in beforeNavigate
? Did it properly update, or does it stay 'stuck' at false
?stormy-goldOP•5w ago
Wait? How does it stay at
false
? I don't get it
Or the state need to be reactive?rare-sapphire•5w ago
I'm asking if it is staying at false or not. Can you log its state to the console?
stormy-goldOP•5w ago
In a sec
Yes? Somehow, it always set to
false
?rare-sapphire•5w ago
How weird. I assumed that svelte would access that value at the time it's called, but maybe it tries to store it somehow.
Try using a reactive version as test (from the Reactivity docs):
this causes rerenders, so I wouldn't call it the final solution. Still, it's worth a try to determine what's going on
stormy-goldOP•5w ago
Okay then?
Lemme try
Oh it worked?
Thanks
I thought the error was the
window.confirm
insteadrare-sapphire•5w ago
I'm almost exclusively a React guy, so I can't confirm why Svelte behaves this way yet. I'll look into it and get back to you though!
This solution will trigger one rerender as soon as a field has been changed. After that, it stays
true
since TanStack Form uses persistent dirty as model. Hopefully that's okaystormy-goldOP•5w ago
Somehow, somehow, I added the
const isDirty = form.useStore((state) => state.isDirty)
And it just works
Weird behaviour
But maybe that should be the best way to have the state
Thank you so much
How should I close this thread?rare-sapphire•5w ago
it'll close on its own after inactivity, or you can right click and click "Close Post"
stormy-goldOP•5w ago
Oh okay
Thanks again
I'm moving away from React to Svelte
But I love TanStack tools so much
rare-sapphire•5w ago
I've always wanted to try it, I just haven't had the time
your issue happened in SvelteKit, right?
stormy-goldOP•5w ago
I'm migrating most of it to Svelte
Yup
rare-sapphire•5w ago
noted! If I learn why it happens, I'll let you know
stormy-goldOP•5w ago
Passionate maintainer. Love your grind and vibe
rare-sapphire•5w ago
in hindsight, it's obvious now.
form.state
is a getter. You generated a new object and assigned it to a variable state
. Of course it will never update that way.
You can avoid that by simply using form.state.isDirty
rare-sapphire•5w ago
https://stackblitz.com/edit/sveltejs-kit-template-default-rdwp4egs?file=src%2Froutes%2Fabout%2F%2Bpage.svelte here's an example of it working as expected
LeCarbonator
StackBlitz
SvelteKit Default Template - StackBlitz
The default SvelteKit template, generated with create-svelte
stormy-goldOP•5w ago
So I don’t need to useStore anymore?
rare-sapphire•5w ago
if you don‘t destructure
state
, yes