I want to set a Vairable only once when a $State variable changes #15600
-
<script lang="ts" >
let data = $state(["hello", "world", "this", "gives", "me", "a", "headache"]);
let isChanged = $state(false)
$effect(() => {
data; // To trigger the rerun on data change
isChanged = true
});
</script>
{#each data as item, i}
<input type="text" bind:value={data[i]} />
{/each}
<p>{isChanged}</p> in this Setup, the value diaplyed for |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
You could add a utility flag: let isChanged = $state(false)
let mounted = false;
$effect(() => {
$state.snapshot(data);
if (mounted)
isChanged = true
mounted = true;
}); Logic like this could be extracted to a separate function in a The callback added in this PR might also help later: |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
You could add a utility flag:
Playground
Logic like this could be extracted to a separate function in a
.svelte.js
file.The callback added in this PR might also help later: