by spuz · 3y ago · 0 comments
Not helping this, it's not particularly well documented. For example, recently I had to learn about the onDestroy function which accepts as an argument a lambda. The docs [1] state that the lambda is run when the component is "unmounted", but offers no further information about exactly how or when this occurs. There is no official documentation on all the various lifecycle stages of a component and how or when each transition occurs. I find myself simply relying on a lot of trial and error or asking in the Discord forum.
I agree with this. Like the example in the linked article about reactive blocks: "Svelte automatically tracks dependencies, and you only need to label a code block as reactive ($:). It'll run the code any time those dependencies change."
There's a huge caveat to that, it only does that with primitives. Which might seem obvious if you've been a developer for a while, but not at all obvious if you are a new developer. React has always taken the approach of asking you to explicitly declare state and use special methods to update it (either with setState or useState when hooks became popular) for exactly this reason, whereas in Svelte sometimes you need to actually reassign a mutated piece of state to itself so it gets picked up by this system like `foo = foo`
I think it's a reasonable tradeoff, because most state can be stored in simple primitives like strings and booleans.
Regarding lifecycle methods and Svelte, it's literally based on the DOM. Is the component leaving the DOM for whatever reason? Then onDestroy will fire. The same as you can treat onMount as a callback that fires as soon as the component is added to the DOM.
https://svelte.dev/repl/b946424a56d84a63a09856bc3df5803e?ver...
I think part of where things get tricky is that Svelte and SvelteKit are so melded together, unless you're 1. only using Svelte and not SvelteKit or 2. Are doing something really advanced and complex, you probably don't need lifecycle methods. And even then, you probably only need onMount.
A custom onDestroy can be used to manually remove something from memory, but it's mostly not needed. I agree though the docs should be better.
idk, i'm dying on the hill that react is unopinionated for a reason, i think it can be morphed and molded to so many use cases
How are the use cases for these libraries different?
It was quite nice to work with, really simple. Though I was just not convinced by its ecosystem, who's gonna use this?
To be fair, React used to have a documentation problem as well, but at this point I think anything and everything under the sun is documented excruciatingly well.
Of course people will comment I'm shilling React, but, reading the top comment, its kind of like trying to shill Svelte but immediately coming up short.