Svelte · Senior · Question 74 of 75

How do you integrate an imperative third-party library (e.g. a chart) cleanly in Svelte 5?

<div {@attach (node) => {
  const chart = createChart(node, options);
  $effect(() => chart.update(data));
  return () => chart.destroy();
}}></div>

Explanation

Only the nested effect re-runs on data changes; the instance is created once.

Further reading

Problem with a question

Svelte · Senior · question 74

Test yourself

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

Take this quiz