T
TanStack5w ago
stormy-gold

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?
No description
18 Replies
rare-sapphire
rare-sapphire5w ago
what is the value of isDirty in beforeNavigate? Did it properly update, or does it stay 'stuck' at false?
stormy-gold
stormy-goldOP5w ago
Wait? How does it stay at false? I don't get it Or the state need to be reactive?
rare-sapphire
rare-sapphire5w ago
I'm asking if it is staying at false or not. Can you log its state to the console?
stormy-gold
stormy-goldOP5w ago
In a sec Yes? Somehow, it always set to false?
rare-sapphire
rare-sapphire5w 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):
const isDirty = form.useStore((state) => state.isDirty)

// ...
beforeNavigate(() => {
console.log(isDirty) // is this still stuck to 'false'?
// ...
})
const isDirty = form.useStore((state) => state.isDirty)

// ...
beforeNavigate(() => {
console.log(isDirty) // is this still stuck to 'false'?
// ...
})
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-gold
stormy-goldOP5w ago
Okay then? Lemme try Oh it worked? Thanks I thought the error was the window.confirm instead
rare-sapphire
rare-sapphire5w 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 okay
stormy-gold
stormy-goldOP5w 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
rare-sapphire5w ago
it'll close on its own after inactivity, or you can right click and click "Close Post"
stormy-gold
stormy-goldOP5w ago
Oh okay Thanks again I'm moving away from React to Svelte But I love TanStack tools so much
rare-sapphire
rare-sapphire5w ago
I've always wanted to try it, I just haven't had the time your issue happened in SvelteKit, right?
stormy-gold
stormy-goldOP5w ago
I'm migrating most of it to Svelte Yup
rare-sapphire
rare-sapphire5w ago
noted! If I learn why it happens, I'll let you know
stormy-gold
stormy-goldOP5w ago
Passionate maintainer. Love your grind and vibe
rare-sapphire
rare-sapphire5w 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
rare-sapphire5w ago
LeCarbonator
StackBlitz
SvelteKit Default Template - StackBlitz
The default SvelteKit template, generated with create-svelte
stormy-gold
stormy-goldOP5w ago
So I don’t need to useStore anymore?
rare-sapphire
rare-sapphire5w ago
if you don‘t destructure state, yes

Did you find this page helpful?