Quantcast
Channel: Active questions tagged svelte - Stack Overflow
Viewing all articles
Browse latest Browse all 1649

Is there a way to simplify this code avoiding to call `onDestroy` manually each time?

$
0
0

We're using EventSource in multiple places in out SvelteKit app, like this:

<script>  import { onDestroy } from 'svelte';  import { customEventSource } from '$lib/events';  const callback = (event: MessageEvent) => {    console.log('event:', event);  };  customEventSource.addEventListener('message', callback);  onDestroy(() => {    customEventSource.removeEventListener('message', callback);  });</script>

As you can see there is a lot to write each time.

Isn't there a magic way - as Svelte has taught us - to simplify?

Especially so I don't have to manually call onDestroy every time?


Viewing all articles
Browse latest Browse all 1649

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>