I created a REPL for a strange issue.
Steps to reproduce
-
Open the REPL
-
Open the browser console
-
Go on the “About” page using the link
-
No message in console
-
Now comment the line:
result = id;
-
And comment out the line:
// setTimeout(() => (result = id), 0);
-
Go on Home page
-
Go on About page
-
Now in the console you can see the message: “handleInput”
Why is this happening?
Is setTimeout
really needed?
Relevant code
- CustomSelect.svelte:
<script>
import Select from 'svelte-select';
export let value = undefined;
export let id = undefined;
$: console.log('CustomSelect, id:', id);
let result;
let items = [
{ value: 'one', label: 'One' },
{ value: 'two', label: 'Two' },
{ value: 'three', label: 'Three' },
];
$: if (id !== undefined) {
result = id;
// setTimeout(() => (result = id), 0);
}
$: if (result != undefined) {
value = { value: 'custom', label: 'Custom' }
}
</script>
<Select {value} {items} on:change on:input />