Nir O.
Nir O.

Reputation: 1651

What guarantees re-rendering in Svelte?

In React, changes to local variables do not guarantee a component (which uses that local variable) re-rendering. Only changes in props or state or context (AFAIK) would guarantee re-rendering.

Is it the same case for Svelte, that is, one must use a proper store to guarantee a re-rendering?

Upvotes: 2

Views: 216

Answers (1)

IamFr0ssT
IamFr0ssT

Reputation: 769

Every assignment to a locally declared variable triggers re-rendering, not just stores.

That is why you need to do something like:

let arr = [];
onMount(() => {
    arr.push(1);
    // A rerender will not be scheduled yet
    arr = arr;
    // Just now, the statement itself will be compiled away, but the runtime will know
});

To guarantee a re-render has been executed before continiuing you can use await tick().

Upvotes: 4

Related Questions