Svelte · Middle · Question 30 of 75

How do you test an effect created outside a component?

const cleanup = $effect.root(() => {
  $effect(() => log.push(count));
});
// ...
cleanup();

Explanation

$effect must run inside a parent effect or component. $effect.root provides that context in tests and long-lived logic.

Further reading

Problem with a question

Svelte · Middle · question 30

Test yourself

Take the Svelte · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz