ciscoheat / sveltekit-superforms

Making SvelteKit forms a pleasure to use!
https://superforms.rocks
MIT License
2.25k stars 65 forks source link

No fine-grained reactivity #490

Open henrykrinkle01 opened 1 month ago

henrykrinkle01 commented 1 month ago

Description Currently I'm using superforms with Svelte 5 just fine. However, when I use $effect or some reactive code on a field input value, it will trigger on every other field updates. I guess because for Svelte 4 reactivity to work, the whole form store must be reassigned. Svelte 5 reactivity is fine grained so it doesn't trigger unnecessary reactions, so this is kind of unexpected when using Svelte 5. Honestly I don't think this is a bug because it's just the way Svelte 4 works. I also don't think this can be easily fixed without migrating the library from using stores to runes. However, @ciscoheat told me to open an issue so here I am.

If applicable, a MRE https://www.sveltelab.dev/fxpfuy1f7nhvp8t

s-petey commented 3 weeks ago

Here is a way that you may be able to work around this issue:

<script lang="ts">
    import { superForm } from 'sveltekit-superforms';

    const superform = superForm({ foo: `I'm getting logged eventhough I don't change`, bar: 0 });
    const { form } = superform;

    const myInspectValue = $derived($form.bar)
    $effect(() => {
        console.log(myInspectValue);
    });
</script>

<button onclick={() => ($form.bar += 1)}> Increment </button>

{$form.bar}
henrykrinkle01 commented 3 weeks ago

That only masks the problem. It doesn't change the fact that $form.foo also gets reassigned whenever bar or any other properties change.

ciscoheat commented 3 weeks ago

It seems like that's how stores work, and as Superforms will keep them for the next major release (as they aren't deprecated, and a rewrite would be too much work), maybe the onChange event could be an alternative.

holdmybeer30 commented 2 weeks ago

It seems like that's how stores work, and as Superforms will keep them for the next major release (as they aren't deprecated, and a rewrite would be too much work), maybe the onChange event could be an alternative.

@ciscoheat Speaking of which, anyone knows if onBlur event will be added soon? Just added the proposal/necessity: https://github.com/ciscoheat/sveltekit-superforms/issues/504

fnimick commented 1 week ago

I also ran into this while trying to do reactive field updates - this is a known breaking change in svelte 5 per https://github.com/sveltejs/svelte/issues/14306