James Nisbet
James Nisbet

Reputation: 394

Svelte: Using reactive statement based on module context variable

I want to use code shared across multiple instances of a component to initiate code within each component.

I've attempted to do this using a reactive statement:

<script context="module">
    let what = 0;
</script>

<script>
    export let number;
    $: if (what === number) [...]
</script>

But changes to what don't trigger a re-run of that reactive statement.

Why doesn't this REPL work, and how can I fix it?

https://svelte.dev/repl/38b94490982f4f3c80644fd364b50723?version=3.16.0

Upvotes: 5

Views: 2050

Answers (1)

James Nisbet
James Nisbet

Reputation: 394

Changing what to a writable seems to fix the problem:

<script context="module">
    import { writable } from 'svelte/store';
    const what = writable(0);
</script>

<script>
    export let number;
    $: if ($what === number) [...]
</script>

https://svelte.dev/repl/f667f3eb6b7d453da1473d5e26268814?version=3.16.0

Upvotes: 10

Related Questions