Closed nkitsaini closed 4 months ago
This sounds like a timing issue - hydration happens too late, so it clears out the value the user has already typed in. This is not strictly related to SvelteKit, more a general limitation of the current Svelte hydration.
A decent workaround is to disable the input until hydration has completed:
<script lang="ts">
+ import { browser } from '$app/environment';
let a = "234"
</script>
-<input bind:value={a}>
+<input disabled={!browser} bind:value={a}>
Ultimately it would be better if the binding was initialized with the input value — not sure if that would cause other problems, but I'd like it if we could do that in a future version of Svelte.
Yeah, the suggested workaround is good enough for my use-case. It is interesting that browser
is false
until hydration.
Transferring to sveltejs/svelte
since there's nothing we can do here
Svelte could perhaps check the value of the binding just after hydration ?
Something like this approximately :
input = claim_element(nodes, "INPUT", {});
// When input is different from the context value
if (input.value !== /*a*/ ctx[0]) {
// We use the binding handler to update it's value
/*input_input_handler*/ ctx[1].call(input).
}
Probably a duplicate of #1755, which tells you how long this problem has been around.
@Rich-Harris Ultimately it would be better if the binding was initialized with the input value — not sure if that would cause other problems, but I'd like it if we could do that in a future version of Svelte.
Did you have a look into this idea already? I think it would be a good thing to implement because this issue is indeed quite annoying and disabling the input until JS loads also seems like a unnecessary delay.
Would be great to have at least the option to enable that via svelte without some hacky workarounds. What do you think? Would love to support you there on that topic, despite svelte code base being new for me :)
A decent workaround is to disable the input until hydration has completed
@Rich-Harris Now that we have nice forms that work without JavaScript via use:enhance
it's a pity that this is the solution, since it renders SSR forms kinda useless. Hopefully this is something that can be resolved in the future by hydrating with the correct values.
closing as dupe of #1755
Describe the bug
If you load a page which contains an
input
element with value binded to a variable, theinput
will reset all the user edits when page load finishes.I'm not sure if this falls exactly into svelte-kit and will this be considered a bug or not. But it sure is little annoying sometimes.
Reproduction
Repo: https://github.com/nkitsaini/svelte-bug-repro Video: https://github.com/nkitsaini/svelte-bug-repro/raw/main/repro.mkv Snippet:
Logs
System Info
Severity
annoyance
Additional Information
No response