Svelte · Middle · Question 56 of 75

What is wrong with this $effect?

$effect(() => {
  if (browser) {
    window.addEventListener("resize", onResize);
  }
});

Explanation

<svelte:window> adds and removes the listener for you. If you do use an effect, return a function that removes the listener.

Further reading

Problem with a question

Svelte · Middle · question 56

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