Live data from Hacker News

Learn how modern JavaScript frameworks work by building one

nolanlawson.com

21–30 of 104 posts

Re: Learn how modern JavaScript frameworks work by building one

#21

> From my perspective, the post-React frameworks have all converged on the same foundational ideas... Using reactivity (e.g. signals) for DOM updates. For an intro from the OG, consider this 6 minute video from Solid [0] or this blog post [1] by Solid's author. 0: https://www.youtube.com/watch?v=cELFZQAMdhQ 1: https://dev.to/ryansolid/building-a-reactive-library-from-sc...

I think Ryan would point you to Knockout as the OG.

Re: Learn how modern JavaScript frameworks work by building one

#22
post #16

I really love Svelte. The compiler is great and very extensible. For example, you can easily add functions to the processing pipeline to process Svelte templates (or the script elements or style sections) in your own special way. It's a fantastic way to build JavaScript frameworks. Svelte people always note Svelte isn't a framework, so this isn't a framework on top of another framework! I used this to build Svekyll,…

I also like svelte but as a compiler-y person who happens to be doing some JS I can't work out why I'm annotating so much stuff by hand (even with svelte 5). I can get why you might want this for react, but isn't svelte a compiler? Can't we do dataflow analysis?

I'm 50% convinced there's a Chesterton's fence I'm mising but where?

Re: Learn how modern JavaScript frameworks work by building one

#23
I like the article, but it gets some things subtly wrong.

> To grossly oversimplify things: React assumes that your entire virtual DOM tree needs to be rebuilt from scratch, and the only way to prevent these updates is to implement useMemo

Not quite, on a state update, it rebuilds the component that was updated and all of its children. Not the entire virtual DOM; old versions of Angular did this, but it was wasteful.

useMemo doesn't prevent that, but React.memo can (useMemo has a different role; it lets you choose when to recompute or recreate a normal JavaScript object. But on its own it won't stop rerendering of child components!) [0]

This invalidates some of their assumptions. The reason why React isn't "push-only" isn't because it does that, it's because it sometimes buffers updates instead of always pushing them immediately. In fact, other frameworks like ~~Svelte also aren't "push-only" and hence not strictly reactive~~! [edit: this is no longer true after Svelte v5, see discussion below] (Funnily enough, OP uses an article as a source that explains this correctly [1], but it seems they took the wrong lesson from it).

The reason why signals are so cool is because the framework knows for any given state change which exact attributes in the DOM need to be re-rendered, even more specifically than "the element and all its children". But this neither implies reactivity nor the other way around. The two concepts are orthogonal.

Anyways, kudos to the author for diving into this so deeply!

[0] useMemo is useful in combination with React.memo sometimes, as the latter compares objects shallowly/by reference instead of their contents, so useMemo can be used to only recreate shallow references if its contents changed. You could probably also reimplement React.memo with useMemo, but you probably shouldn't. [1] https://dev.to/this-is-learning/how-react-isn-t-reactive-and...

Re: Learn how modern JavaScript frameworks work by building one

#25
post #2

I'm always surprised when articles mention a bunch of JS frameworks and leave out RiotJS. I can't be the only person using it?

Never heard of it. Looks sveltish.

I feel like they are all very similar. I found RiotJS before Svelte. Somehow it never caught a hype-train like others did.

I suppose, if which ever choice is doing the trick - keep it while practicable

Re: Learn how modern JavaScript frameworks work by building one

#27
Question to the folks with a lot of frontend framework experience:

Is there a framework/library that supports the usage of an effect-system when it comes to rendering actions?

For instance, in react, a component (or rather it's render-function) has to return the element(s) directly. Is there a framework where the render-function accepts something effect-like or promise-like instead, even if that means that the rendering might potentially be delayed?

Re: Learn how modern JavaScript frameworks work by building one

#28

Question to the folks with a lot of frontend framework experience: Is there a framework/library that supports the usage of an effect-system when it comes to rendering actions? For instance, in react, a component (or rather it's render-function) has to return the element(s) directly. Is there a framework where the render-function accepts something effect-like or promise-like instead, even if that means that the render…

React now supports this using Suspense boundaries [0]. Some frameworks (eg. NextJS) already ship variants of it but here is some code you could use in React's development version:

    function MyComponent() {
      const promise = ...;
      const result = use(promise);  // use is like await
      // do something
    }

    function Wrapper() {
      return (
        }>
          
        
      );
    }

You don't need to know this to use it, but the implementation is both interesting and horrifying: The `use` hook checks if the Promise has resolved, and if not, it throws an error that is caught by the Suspense boundary. And because there is no property like `hasResolved` on JS promises, `use` adds one itself. (At least this was how an early draft proposed it, a lot of changes have been done since, and my knowledge might be out of date.)

[0] https://react.dev/reference/react/Suspense

Re: Learn how modern JavaScript frameworks work by building one

#29
post #23

I like the article, but it gets some things subtly wrong. > To grossly oversimplify things: React assumes that your entire virtual DOM tree needs to be rebuilt from scratch, and the only way to prevent these updates is to implement useMemo Not quite, on a state update, it rebuilds the component that was updated and all of its children. Not the entire virtual DOM; old versions of Angular did this, but it was wasteful.…

Author here. Thanks for the thoughtful reply!

I did indeed mix up `useMemo` and `React.memo` – fixed it in the post.

You're right, I am skipping a lot of details (hence "to grossly oversimplify"). I know that React doesn't invalidate the whole tree, but it does in the worst case. Maybe I should add a note about that.

Svelte not being truly reactive makes perfect sense, but in Svelte v5 my understanding is that "runes mode" does exactly that. This is what I mean by "moving in that direction."

Re: Learn how modern JavaScript frameworks work by building one

#30

Question to the folks with a lot of frontend framework experience: Is there a framework/library that supports the usage of an effect-system when it comes to rendering actions? For instance, in react, a component (or rather it's render-function) has to return the element(s) directly. Is there a framework where the render-function accepts something effect-like or promise-like instead, even if that means that the render…

If you mean effects as data, I can't think of anything off top other than elm which is a language + framework.

I've made my own though: https://github.com/marcellerusu/capable-js.

Its not for use but it was an interesting experience that enables a lot of new patterns by using generators.

I don't claim that it is better than other frameworks though, there's a lot of times where this pattern is significantly more cumbersome than just using react.

Post reply on HN