Svelte reactive statement which updates when only some of the referenced variables are changed

Viewed 473

I have been using Svelte for a while and this issue really bugs me. How could I create a reactive statement which updates when only some of the referenced variables are changed?

For example, I want to re-compute d only when a or b are changed, but not when c is changed:

<script>
    let a = 1;
    let b = 2;
    let c = 3;
    $: d = a + b + c;
</script>

But d is re-eveluated when a,b, or c changes.

How could I do that?

1 Answers

You can use function that accepts a and b and closes over c:

function update(a, b) {
  return a + b + c;
}

$: d = update(a, b);
Related