Quantcast
Channel: Active questions tagged svelte - Stack Overflow
Viewing all articles
Browse latest Browse all 1541

How to skip initial reactivity for a variable within svelte components?

$
0
0

I have a simple custom component in Svelte, where I use reactivity to execute some code.

<script lang="ts">    export let liked: boolean    $: {         //..code that should be executed when liked is toggled ...//    }</script>

My problem is that when the component first mounts liked is toggled and the code is executed which is not what I want, I want to execute the code only when the user initiate the toggle.

That is why I added this initial variable here, which is making my code work as expected, but was wondering if there is a more idiomatic/clean way to do this?

<script lang="ts">    export let liked: boolean    let initial = true    $: {        if (!initial) {            //..code that should be executed when liked is toggled ...//        } else {            initial = false        }    }</script>

Viewing all articles
Browse latest Browse all 1541

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>