Chris Talman
Chris Talman

Reputation: 1169

Can changes be evaluated for bound checkboxes?

In Svelte, you can bind the checked attribute of a checkbox. You can then observe that bound value on the component, the callback of which provides the current and previous values.

However, when I try to evaluate changes in the state of a checkbox, the current and previous values seem identical. Please see this example to illustrate the problem. Have I done something wrong here?

Upvotes: 1

Views: 587

Answers (1)

Rich Harris
Rich Harris

Reputation: 29615

No, you haven't done something wrong — this is a quirk of how observers work, and the mutable nature of objects and arrays in JavaScript.

The observer is observing the object rather than the checked property, and the object itself isn't actually changing. Instead, you would need to observe item.checked directly.

That's not possible with the built-in observe method, but you can do it with observeDeep in svelte-extrashere's a demo.

Upvotes: 2

Related Questions