-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Closed
Description
Describe the bug
I want to derive the value of a checkbox from a state.
When an onclick handler (with preventDefault) changes its own checked state, it does not seem to be applied.
This seems like a bug.
If it is intended behavior, can anyone point out a reason or explanation?
The reason why I need the preventDefault() in my production use case is that I would like to only set the checked state if other conditions are met, not just as a direct consequence of the click.
<script>
let on = $state(true);
const toggle = () => {
on = !on;
}
</script>
<h1>Switches! on={on}</h1>
<label>
<input
checked={on}
type=checkbox
onclick={(evt) => {
evt.preventDefault();
toggle();
}}
>
on
</label>
<br>
<button onclick={() => toggle()}>
{on ? "on" : "off"}
</button>
Reproduction
Steps to reproduce the issue in the REPL:
- click on the checkbox
- observe that it changes the state in the header and button
- observe that it does not check the checkbox (whose value is derived from the same state)
Logs
System Info
see REPL URL:
Svelte `version=5.38.6`
reproducable without any special setupSeverity
annoyance
Metadata
Metadata
Assignees
Labels
No labels