News for hackers. Stuff that matters.
Re:RFC: Intent to Ship React 18 by xiphias2 on Thursday March 24, 2022 @01:50PM (#30793231)
Rich Harriss, the author of Svelte talked about all the features that come to React to speed up virtual dom comparisions, but to me he's framework proved it that virtual dom is not necessary, and not the best solution for a scalable UI.

At the same time there's a huge amount of React code at this point. It would be an interesting experiment to try to compile React code to really reactive code that doesn't need virtual DOM.

Re:RFC: Intent to Ship React 18 by tehbeard on Thursday March 24, 2022 @02:18PM (#30793537)
I just don't know, svelte feels way too similar to the "magic" of knockout and angular 1.0 when looking at the output... Something I got burnt by with weird edge cases of update loops or desync.

Svelte might have that all reasoned out though, in which case the harder separation of logic and view is the only stickler for me, and transpiling JSX into svelte would be interesting to see.

Re:RFC: Intent to Ship React 18 by dham on Thursday March 24, 2022 @02:32PM (#30793693)
If you think Svelte is magic, wait until you see how hooks work.
Re:RFC: Intent to Ship React 18 by nightski on Thursday March 24, 2022 @02:52PM (#30793915)
Svelte changes the semantics of your code. It's great to show off to-do apps and simple examples. But I'd be very wary of buying into a framework that relied on compiler modifications to the semantics of code to make it work. I'm also not a fan of pub-sub style change notifications. It's hard to reason about what a change in state is going to cause since you don't know what's subscribed.

With hooks at least I am able to have a mental model around what happens when I call a change function since it flows top down (instead of bottom up).

Re:RFC: Intent to Ship React 18 by dmitriid on Thursday March 24, 2022 @04:30PM (#30794988)
> Svelte changes the semantics of your code.

So do hooks. Suddenly regular function calls can't be put in if statements or require "dependency lists" to make them work properly.

Re:RFC: Intent to Ship React 18 by Spivak on Thursday March 24, 2022 @04:42PM (#30795089)
Ehh it really doesn't change the semantics of your code, hooks are just iterables and you access them by calling next(). In an alternative world they would be a mapping that you give unique names like useState("mystate", ...) and then order and if statements would be irrelevant.

Also you're just just passing a callback function to React and then giving it some pointers to data used as part of the conditional on when it should run.

Like it's janky and reaches through layers but it's not up and rewriting your code.

Re:RFC: Intent to Ship React 18 by dmitriid on Thursday March 24, 2022 @04:44PM (#30795106)
> hooks are just iterables and you access them by calling next()

In Javascript, "just iterables" can be put in conditional statements, and they don't cause the outer function to be called again.

> Also you're just just

Ah yes. That "just" again

Edit:

> just passing a callback function to React and then giving it some pointers to data

Thing is, in plain JavaScript if it was a callback function, it could be called anything and live anywhere. However, these functions have to be named with a `use` prefix and have limitations on how they can be called.

Re:RFC: Intent to Ship React 18 by Spivak on Thursday March 24, 2022 @05:01PM (#30795287)
And you can put hooks in if statements too, but like iterables it won't have the effect you probably want.

    it = ["hello", "world", "foo", "bar"]

    shouldbehello = next(it)
    shouldbeworld = next(it)

    if sometimes_true_sometimes_false:
       shouldbefoo = next(it)

    # if the conditional is false this will be foo, oops.
    shouldbebar = next(it)
Hooks aren't callback functions, they're functions that take callback functions. apparently this is breaking python. The naming convention is just so the linter can identify hooks, they're not magic.

    def react_to_stuff(func, var):
       orig = var.copy()

       def monitor():
          while True:
              sleep 2
              if var != orig:
                  orig = var.copy()
                  func(var)

       t = Thread(target=monitor)
       t.start()

     stuff = "Hello"
     react_to_stuff(lambda x: print(x), stuff)
Re:RFC: Intent to Ship React 18 by dmitriid on Friday March 25, 2022 @03:33AM (#30799546)
No idea why you're giving examples in Python code when we're talking about Javascript and React.

On "it's iterators actually", I have a response to a sibling comment here: https://news.ycombinator.com/item?id=30799543

Re:RFC: Intent to Ship React 18 by Spivak on Friday March 25, 2022 @08:49AM (#30801512)
Because it’s better pseduocode and the concept isn’t language dependent.
Re:RFC: Intent to Ship React 18 by charcircuit on Thursday March 24, 2022 @04:59PM (#30795257)
Nothing is stopping you from putting useState in an if statement. You just have to be careful since it's an "iterator." For example if you put it in an if branch you will want to also use it in the else branch so "next" is called the same number of times.
Re:RFC: Intent to Ship React 18 by Osiris on Thursday March 24, 2022 @10:09PM (#30797960)
In my experience I get a React error if the number of hooks changes between renders, meaning you can't have hooks in a conditional.
Re:RFC: Intent to Ship React 18 by dmitriid on Friday March 25, 2022 @03:33AM (#30799543)
So, let me get this straight.

- These are "just function calls"

- Except they are "iterators"

- Except if you have them in a conditional, you must provide an `else` branch so that `next` is called the same number of times

None of this is in the semantics of the language where I don't have to provide an else branch to an iterator, and function calls are not iterators ;)

Re:RFC: Intent to Ship React 18 by Spivak on Friday March 25, 2022 @09:02AM (#30801622)
Like I really don’t know how to explain this any clearer.

* React stores the state for your hooks in a list which is iterable.

* useBlah is just a normal function that internally calls next() on that list to get the next hook state which is why order matters.

* React checks that the iteratior on the list is at the end to see if you’ve consumed all the hooks and throws an error if not because it’s indicative of a bug in your code.

Like at this point the only thing I can recommend is try writing a toy implementation of hooks and see that they’re not magic, they’re just normal JS, and that the restrictions flow naturally from the implementation.

Like why do you want so bad for hooks to be weird?

Re:RFC: Intent to Ship React 18 by dmitriid on Friday March 25, 2022 @01:00PM (#30804575)
> is just a normal function that

you keep showing isn't a normal function :)

> Like why do you want so bad for hooks to be weird?

I don't want them to be weird. They are weird already. https://news.ycombinator.com/item?id=30801466

Re:RFC: Intent to Ship React 18 by Spivak on Friday March 25, 2022 @01:39PM (#30805064)
I guess we have very different definitions of "semantics" then. Rspec is wild but it's still plain Ruby. I'll give you that it's not idiomatic and it comes with some unusual restrictions on its use but libraries have been doing forever, I guess I'm just used to "you have to call this function to init the lib", "this function has to come after this", "you must call the cleanup macro here", "importing this lib monkey-patches these classes."
Re:RFC: Intent to Ship React 18 by jitl on Thursday March 24, 2022 @03:38PM (#30794412)
You can implement a hooks like interface in an afternoon. I don’t think I could do the same for Svelte’s compiler-oriented approach. Using a compiler means that Svelte is essentially a dialect of EcmaScript, which appears to be mutually intelligible but isn’t always. React used a compiler for JSX which also introduced a dialect, but JSX is optional and very straightforward sugar for a simple function call. The Svelte dialect is not so simple.
Re:RFC: Intent to Ship React 18 by dmitriid on Thursday March 24, 2022 @04:33PM (#30795012)
> Using a compiler means that Svelte is essentially a dialect of EcmaScript

So are hooks. The fact that you can't put regular function calls (which hooks look like) in an if statement, or have to provide custom "dependency lists" to some of them, or that data returned from them suddenly re-renders (aka calls again) the function they are in tells you that this is no longer regular Javascript.

Re:RFC: Intent to Ship React 18 by dham on Thursday March 24, 2022 @06:01PM (#30795888)
Exactly, I don't get the React is just Javascript. It's not. You're tied to React just as much as Svelte or anything else. The Facebook propaganda machine is clever I will say.
Re:RFC: Intent to Ship React 18 by jitl on Thursday March 24, 2022 @07:53PM (#30797072)
There’s a wide difference between learning the semantics of an API and learning the semantics of a custom language with a compiler. There are alternate implementations of the React API, like Preact. The semantics of the React API might be complex, but you could implement them in essentially any programming language with function pointers and variable length arrays. You can’t say the same thing for Svelte; which we know is impossible to implement in pure JavaScript, which is why it needs a compiler. This is all anyone in this thread is arguing - it’s an argument about semantics but for some kinds of environments semantics are very important.

I’ve helped teams pilot out of own-the-world frameworks like Backbone and Rails before (both of which substantially distort the runtime they’re embedded in); but it was much harder to deal with Backbone + Coffeescript together. For Backbone w/ vanilla JS or Rails, we could use standard static analysis tools, Coffeescript added a big extra wrinkle to our migration.

Re:RFC: Intent to Ship React 18 by brundolf on Thursday March 24, 2022 @07:48PM (#30797019)
Yes and no. They definitely push the boundaries, and the framework imposes certain extra restrictions on how it wants you to use them, and they warp your normal intuitions a bit. But at the end of the day they are just function calls. They don't compile down to some other sort of construct with totally different language semantics.

There's a fundamental difference between a framework that takes your code and does weird things with it, vs a framework that rewrites the semantics of your code out from under you.

Re:RFC: Intent to Ship React 18 by dmitriid on Friday March 25, 2022 @08:45AM (#30801466)
Yes, they push the boundaries and essentially create a DSL with its own rules that do change semantics of the language. Not as drastically as a custom compiler, but they still do.
Re:RFC: Intent to Ship React 18 by brundolf on Friday March 25, 2022 @03:52PM (#30806411)
They specifically do not change the semantics of the language, they create a new abstraction on top of it that has unusual semantics.
Re:RFC: Intent to Ship React 18 by tehbeard on Thursday March 24, 2022 @05:54PM (#30795795)
A fair point, and I haven't dove into react's inner workings to see how the sausage (I was more discussing the compiled code of the app), but one can reason "how" hooks work after observing them.

    - They're tied to the lifecycle of the component, so they must reach "up" into whatever just called our component function.
    - They do not like being moved about/conditionally executed, so hook's likely use an array for metadata storage and position rather than needing explicit "hook keys".
Honestly they are weird, but I can reason about them with a "good enough" mental model to get paid.

I've no clue what svelte does behind the scenes, much the same way as I can't reckon what WASM or assembly do. So I guess I just have to blindly trust the compiler?

Re:RFC: Intent to Ship React 18 by thatswrong0 on Thursday March 24, 2022 @08:47PM (#30797499)
Agree with you completely - hooks really aren't magical at all. The only thing that's "magical" is that React takes hook data and embeds it somewhere in the component instance behind the scene.. something that also necessarily must happen with class components. The state has to live somewhere.
Re:RFC: Intent to Ship React 18 by thatswrong0 on Thursday March 24, 2022 @08:41PM (#30797463)
I'm always confused by this comment about hooks. You can sus out the basics of how hooks work under the hood by just looking at their relatively simple rules that govern their usage. More from Dan Abramov: https://medium.com/@dan_abramov/making-sense-of-react-hooks-...

I think they are actually _more_ explicit than classes about how the state is stored. IMO `this` in class based components isn't as straightforward (e.g. https://overreacted.io/why-do-we-write-super-props/)

Re:RFC: Intent to Ship React 18 by badbanana on Thursday March 24, 2022 @01:59PM (#30793350)
Not React code but SolidJS uses JSX to produce reactive code compile time in the same vein as Svelte.
Re:RFC: Intent to Ship React 18 by josephg on Thursday March 24, 2022 @02:36PM (#30793732)
Yeah I threw together a simple wiki on top of a CRDT using solidjs a couple weeks ago. It’s delightful - it’s basically react but compiled to be smaller and faster. (It looks and feels like react but there’s no giant runtime, and no vdom diffing). And it has a really nice API for reactive state.

I haven’t built anything big with it yet but I like what I see so far!

Re:RFC: Intent to Ship React 18 by daok on Thursday March 24, 2022 @02:11PM (#30793486)
IMP SolidJS fixes a lot of issue React as without leave the "React-ish" way to build components.
Re:RFC: Intent to Ship React 18 by picardo on Thursday March 24, 2022 @02:33PM (#30793699)
What Rich Harris said sounds very nice and interesting, but I'd take issue with it being "not the best solution for a scalable UI." As long as the slowdown caused by the Virtual DOM remains below limits of human perception, it doesn't really matter whether something can do without Virtual DOM or not.
Re:RFC: Intent to Ship React 18 by judofyr on Thursday March 24, 2022 @02:44PM (#30793819)
I’ve worked on React applications where a single key press would cause lags over 300ms. The React profiler wasn’t able to show any latency in my render functions and using the browser profiler I saw that all time was spent somewhere inside React’s internal.

There’s some real overhead of React’s virtual DOM.

Re:RFC: Intent to Ship React 18 by picardo on Thursday March 24, 2022 @02:57PM (#30793981)
I've worked on dozens of large React codebases, and never once came across a performance problem that was caused by VDOM overhead. Performance lag was always caused by bad application design. In most cases, the performance lag was due to over-rendering caused by a badly designed component architecture and state management system.
Re:RFC: Intent to Ship React 18 by brundolf on Thursday March 24, 2022 @02:50PM (#30793891)
That's bizarre; I've spent a lot of time spot-optimizing React apps and never had something like that happen

The VDOM definitely carried overhead in my case, but it was easy to profile and optimize (with the React tools). In many cases, reflowing the sheer size of the DOM we were generating was a bigger bottleneck than the time it took React to render it in the first place

I'm really curious what was going on in your app

Re:RFC: Intent to Ship React 18 by danielvaughn on Thursday March 24, 2022 @03:59PM (#30794648)
I've seen it a lot, personally. I've even seen instances where the input would delay by over a second. Really wild stuff.

Can a better design help to mitigate those issues? Sure. But I don't like having to wonder whether it was my own design, or if it's something internal to the library.

Re:RFC: Intent to Ship React 18 by brundolf on Thursday March 24, 2022 @05:05PM (#30795340)
I think we're talking about two different things- I've definitely seen that much delay before, but it always came down to inefficient rendering logic and/or components that re-rendered excessively often, both of which are easy to detect in profiling and usually not too hard to solve

This is the part I thought I was bizarre:

> The React profiler wasn’t able to show any latency in my render functions and using the browser profiler I saw that all time was spent somewhere inside React’s internal

Re:RFC: Intent to Ship React 18 by bastawhiz on Thursday March 24, 2022 @08:58PM (#30797562)
There's nothing inherently slow about React internals. It does exactly the work you tell it to do. If you tell it to do too much unnecessary work, it'll happily go do that.

Svelte avoids this by magically not doing the unnecessary work. That doesn't mean it's faster, it just means your design needs to make different kinds of considerations.

Re:RFC: Intent to Ship React 18 by bastawhiz on Thursday March 24, 2022 @08:55PM (#30797543)
I'm going to call shenanigans on this. I've worked on dozens of applications (my own and others) with hundreds of thousands (and even millions) of DOM nodes. I've never seen a problem like this. React simply doesn't rerender things that you don't tell it to. It's easy to try to do something clever and have it cause unnecessary (and expensive) rerenders, but that's not the fault of React internals being slow, it's the fault of the userspace code doing more work than it needs to. Of course the profiler shows the time is spent inside React, it's doing all the work your code told it to do.

React also gives you great tools to avoid and remediate this (memoization wrappers, the dev tools highlight elements that rerender, linters that check you're using hooks correctly), so it's really hard to say that this is a slowness that's endemic to the vdom.

Re:RFC: Intent to Ship React 18 by tshaddox on Thursday March 24, 2022 @05:11PM (#30795389)
I would be very, very surprised to see React code that can take 300ms in the virtual DOM unless it's deliberately designed to do so. To be honest, I've built a whole lot of stuff in React and it's not even immediately clear to me how I would deliberately design an app to take 300ms in the virtual DOM.
Re:RFC: Intent to Ship React 18 by MuffinFlavored on Thursday March 24, 2022 @03:40PM (#30794450)
Does anybody use https://preactjs.com/ seriously?

I thought React and React-DOM were separate?

https://www.npmjs.com/package/react

https://www.npmjs.com/package/react-dom

Re:RFC: Intent to Ship React 18 by richeyryan on Friday March 25, 2022 @05:26AM (#30800076)
I have used it on production projections and it's seamless. It works with third party React packages with no problems whatsoever and is much lighter. For a stock standard SPA with no need for the upcoming server rendering stuff in React, it's excellent!
Re:RFC: Intent to Ship React 18 by vmware513 on Friday March 25, 2022 @02:07AM (#30799123)
Luckily, there is Ember.js, which provides the best developer and user experience for a decade now, with smooth and consistent update and improvements.

Still the best choice for huge, corporate frontend projects.