Live data from Hacker News

Thoughts on Svelte

tyhopp.com

61–70 of 194 posts

Re: Thoughts on Svelte

#61
I've been building an app with Sveltekit (ok, two); and I've proposed a talk at THAT conf in the summer. I agree with the author's positive and negative assessments, but really for me the developer experience outweighs the negatives.

In VS Code I turn on seeing short directories in the tabs, which prevents file confusion. Beyond that it's been a joy to use and pretty easy to get going with. Any confusion or learning comes from not doing next.js first and having to learn about which things happen where in terms of client and server. It's eye opening though and it isn't forced learning.

I give Sveltekit a "you should certainly consider it" vote.

Re: Thoughts on Svelte

#62
post #26

I switched my game's UI from Vue2 to Svelte maybe a year ago, and I agree with almost all of the article. One thing I think the author overlooks: the appeal of Svelte's built-in animations and transitions is that they work correctly when the element's lifecycle is being managed by Svelte. E.g. with code like this: {#if showFoo} Foo! {/if} When `showFoo` changes to false, Svelte will first play a flyout transition and…

It is actually not that hard in CSS plus vanilla JS using stuff like the `animationend` event (https://developer.mozilla.org/en-US/docs/Web/API/Element/ani...). Of course svelte provides a much easier interface, then having to have to deal with event listeners.

Re: Thoughts on Svelte

#64
post #27

Earlier quoted context omitted.

I guess my main gripes are not with the libraries for state management themselves, but how managing rendering after state mutations occur. Pinia and Vue/Nuxt just do this effortlessly. Having to deal with hooks and useEffect in React is just a pain... in my opinion.

You shouldn't use useEffect just to bring state mutations from the store into the component or to calculate "view model" from your state. Have a look at the new react docs. https://react.dev/learn/you-might-not-need-an-effect

But then again, you might need an effect. The behind the scenes (or behind the hooks) complexity that react adds in order to the make DOM thing work then makes it difficult when you do need an effect. Things like WebRTC or sockets or some such. useRef? useState? I have found this to be extremely difficult. If someone knows a good resource for doing things like this in React (and not a one page file, but with multiple components etc) I would appreciate knowing about it.

Re: Thoughts on Svelte

#65
post #26

I switched my game's UI from Vue2 to Svelte maybe a year ago, and I agree with almost all of the article. One thing I think the author overlooks: the appeal of Svelte's built-in animations and transitions is that they work correctly when the element's lifecycle is being managed by Svelte. E.g. with code like this: {#if showFoo} Foo! {/if} When `showFoo` changes to false, Svelte will first play a flyout transition and…

Co-opting my comment to add another thought. For me, Svelte's big superpower is that it does a great job of looking like plain JS sitting next to templated HTML, and the two just magically react to each other.

The reason this is huge is because 90% of my coding is not Svelte. Usually I'm working on app logic, so I might go weeks or occasionally months without touching any UI code. With Vue I used to dread making UI changes, because I always wound up needing to re-learn which properties I needed to put in which return value to which callback (or somesuch, I don't remember the details). I'm sure it's very intuitive if one uses it every day, but it never was for me after a six week gap.

In contrast with Svelte I feel comfortable doing UI work after a long hiatus, even with nontrivially reactive bits, because apart from the templating it's basically just plain JS (or looks like it). It took a bit of bother initially architecting stuff, but I don't think I've checked its docs since I first switched to it.

That said, the big tradeoff is that Svelte's reactivity is quite magical. Personally this doesn't bother me, because as a JS main I find it easy to intuit what Svelte must be doing behind the scenes in order to work the way it does. But if you're an occasional JS user you might hate Svelte's magicalness for the same reason I disliked Vue - e.g. you might keep needing to re-learn when to use $: statements, etc.

Re: Thoughts on Svelte

#66
post #45
post #22

Earlier quoted context omitted.

I don't get it either. Plus, what exactly is so hard about using hooks? Hooks are awesome.

> what exactly is so hard about using hooks Oh, this I can tell. For one thing, it's very easy to get stale closures if one isn't careful. For another, hooks are a reactivity mechanism that is tied to the re-rendering of the whole component (what if you don't want to re-render the component? what if you only want to perform a side effect when a particular value changes?). Third, the docs are sowing confusion by disco…

> what if you only want to perform a side effect when a particular value changes?

https://react.dev/reference/react/useEffect

That is literally what useEffect is for! Describe your side effect, provide a list of values that you want the hook to watch for when they change. `useEffect(someEffect, [value1, value2, value3])`

> the very natural, and often inevitable, concept of side effects

React uses a functional programming model. Always has. Watch the Honeypot React documentary and notice how many times the earliest adopters say they were excited to have way to express UIs functionally. Functional programmers believe in minimizing side effects. If you don't like the paradigm, there are 1000 other UI frameworks that use an imperative model. Complaints like yours read like someone complaining that their screwdriver isn't a hammer.

Re: Thoughts on Svelte

#67

Now I notice I was doing: Rip out {#await ...} and put it in the logic, then use local variables when rendering. intuitively and without realizing. Thanks for writing about it.

We don't use {#await because it has several issues. We use this utility to access promises in Svelte components, converting promises to stores:

    export type PromiseState =
        | { status: 'loading'; value?: undefined; error?: undefined }
        | { status: 'resolved'; value: T; error?: undefined }
        | { status: 'rejected'; value?: undefined; error: Error };
    
    export function fromPromise(promise: Promise): Readable> {
        return readable>({ status: 'loading' }, (set) => {
            void promise
                .then((value) => set({ status: 'resolved', value }))
                .catch((error) => set({ status: 'rejected', error }));
        });
    }

Re: Thoughts on Svelte

#68
post #4

I share your view that Svelte is a great framework for small projects. It is also really useful if you want to build small standalone javascript widgets. I've used it at work to build a fast video testimonial slider for Shopify [1]. I really liked the component format as we could quickly integrate our code in the existing HTML layout. The Svelte built-in store has also been useful for us to manage the current state o…

May I ask you how did you find the process of programming a Shopify app using Svelte? I have some experience with Svelte and am currently in process of starting development of a new Shopify app, but I basically decided to go with React as they have a lot of documentation and libraries (Polaris, AppBridge) available for it, while on Svelte I would have to do everything from scratch (or at least that's how I understood it from their docs)

EDIT: after reading your comment one more time, I assume you've used Svelte only for a small part of the app, not the app as a whole?

Re: Thoughts on Svelte

#69
As a Svelte user, and someone who definitely prefers it over the old frameworks, the praise and the criticisms seem accurate:

> In the end I found it was difficult to determine reliably when to reach for the $ label. I'd use it in one scenario and it seemed to work like I expect, then throw it at another scenario and it didn't work like I expect.

Yep. There's a bunch of different syntaxes and I can't find a documentation page that lists all of them. I use it, but I don't have confidence to always know when it's appropriate.

> Rip out {#await ...} and put it in the logic, then use local variables when rendering.

Yep. Handling promises isn't something to be done in the HTML. HTML should deal with values.

I think even given these Svelte is still the obvious choice for new apps. Particularly the cleaner syntax, less code, batteries-included handling of CSS, and stores. But the two points above would be great to improve for future versions.

Re: Thoughts on Svelte

#70

I work with React (NextJS) and from working on things in Vue, and Nuxt - the one thing I absolutely hate about React is state management. If you have never used Pinia[0] (Vuex) with Vue, it's just so, so, so much easier. I'm using Zustand[1] with React as it is as similar as I can find to Pinia, but the whole hook system is just painful to work with... OK rant over. I haven't built anything substantial with Svelte, b…

I went through similar troubles, then ended up trying [react-query](https://tanstack.com/query/v3/) and now I don't bother anymore. React Query does everything I need, and if for some reason I still need separate state management, I use useContext for those parts

React Query is a joy to use and clicks very nicely with how I am structuring my apps, building around interacting with async things like APIs

Post reply on HN