Reputation: 351
I have two reactive statements in Svelte. When the child statement changes (based on some input), the parent statement (statement on which the child statement depends on) is also triggered. Check the following snippet:
<script>
const options = [
{key: 'one'},
{key: 'two'},
{key: 'three'},
];
// NOTE: initialOption may be obtained reactively from somewhere like the query string.
$: initialOption = options[0];
$: selectedOption = initialOption;
$: console.log('initialOption reactivity:', initialOption);
$: console.log('selectionOption reactivity:', selectedOption);
</script>
<select bind:value={selectedOption}>
{#each options as opt}
<option value={opt}>{opt.key}</option>
{/each}
</select>
https://svelte.dev/repl/bdc48c75acf94a9ca13da377b7cd8f19?version=3.20.1
If you run the snippet above, each time you try to select from the drop-down (which is bound to selectedOption
), the initialOption
assignment seems to be re-triggered, why? Is this expected behavior?
Upvotes: 1
Views: 485
Reputation: 9979
The issue is two-fold:
value
property of your select
elementWhen you interact with your select
input, you update the value of selectedOption
, which in turns updates initialOption
(because both variables are references to the same object).
You have two ways to solve this issue:
Both approaches are demonstrated in this REPL
Upvotes: 1