4 ms·
For sure. Look at this Svelte 5 example: <script> let time = $state(new Date().toLocaleTimeString()); $effect(() => { const timer = setInterval(() =>
by davedx 3y ago
For sure. Look at this Svelte 5 example:
<script>
let time = $state(new Date().toLocaleTimeString());
$effect(() => {
const timer = setInterval(() => {
time = new Date().toLocaleTimeString();
}, 1000);
return () => clearInterval(timer);
});
</script>
<p>Current time: {time}</p>
- onsclom 3y agoThis example is a bit strange to me. I wonder if `$effect` will actually replace all usage of `onMount` and `onDestroy` for Svelte. In Solid you could totally do these things with `useEffect` too, but they chose to keep `onMount` and `onDestroy` (which they call `onCleanup`) to make this kind of thing more simple. Example: https://www.solidjs.com/examples/counter https://www.solidjs.com/examples/counter `onMount` and `onDestroy` feel like really useful, dependable callbacks. `$effect` is scary because if you add a reference to state in it, you can't depend on it being called like `onMount`.
- madacol 3y agoThey don't claim they'll remove `onMount`. They claim most (but not all) of current usage of `onMount` will be better off using `$effect` instead