How to access lifecycle methods in universal reactivity? #15350
Unanswered
daviareias
asked this question in
Q&A
Replies: 1 comment 1 reply
-
If the code is supposed to exist in the same lifecycle as a component, you can just use the various hooks and the <script>
import { counter } from './counter.svelte.js';
const getCount = counter();
</script>
Count: {getCount()} // counter.svelte.js
export function counter() {
let count = $state(0);
$effect(() => {
console.log('initializing interval');
const handle = setInterval(() => count++, 1000);
return () => {
console.log('cleaning up interval');
clearInterval(handle);
}
});
return () => count;
} You could use If you want something that is not bound to the lifecycle of a component you would use |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm having a hard time finding out how to clear a interval/timeout using "Universal reactivity" files.
It's also really hard to search information regarding "svelte.js/svelte.ts" files because the only name referring to them is "universal reactivity" which is not great for search engines and sounds generic.
I tried doing something like this but doesn't seem to work:
https://v4.svelte.dev/tutorial/ondestroy
using $effect.root can maybe work, but there's no clear way to figure you when to call clean up, since we don't know inside svelte.js files when the component using the reactivity logic is being destroyed or mounted.
Beta Was this translation helpful? Give feedback.
All reactions