News for hackers. Stuff that matters.
Re:The self-fulfilling prophecy of React by unconed on Thursday September 08, 2022 @08:38AM (#32764625)
I couldn't disagree more.

What all these React spinoffs seem to be doing is unlearning the lessons encoded in the React architecture and replacing them with more magic by "helpfully" applying state changes immediately instead of in a controlled fashion. But this is going to create mixtures of old and new state, which creates subtle bugs down the line.

Discussions about VDOM also seem mostly pointless because it is clear to me most people's mental model of how React renders is flat out wrong. If you are redundantly touching the DOM, that means you haven't memoized your components, or you are needlessly making copies of the same state. All of these have far bigger performance implications. In fact, even without memo(...), React renders can halt early.

Quoting from the Use.GPU/Live docs, which is a true React extension with the same semantics:

"If a component renders the exact same JSX object as before, React will not re-render that child, even if the child is not memoized.

So there is a hidden semantic in React: when you construct a JSX expression anew, you are requesting that those children be re-rendered unless memoized. But if you reuse a JSX expression from before, you are implicitly giving permission to ignore any unchanged children."

This is crucial to understand when stacking context providers.

I don't get the sense that React derivatives have been used to build e.g. desktop-class apps. They mostly seem optimized for web sites.

While it is true that React does a lot of things at run-time instead of build time, this is necessary for building truly data driven apps. JSX is really Lisp quoting in disguise. If you can't recognize this, you should probably brush up on the fundamentals.

Re:The self-fulfilling prophecy of React by cageface on Thursday September 08, 2022 @08:23PM (#32773830)
Applying state changes in a controlled fashion is just about the last quality I'd ascribe to a non-trivial React codebase. By the time you've finished with all the ceremony of memoizing everything just right so you get acceptable performance you're left with a brittle codebase full of surprises. Making one innocuous change can break all your memoizations. Or you're just as likely to find something failing to update because it's been over memoized. Manually managing all the dependency arrays for all this is very poor DX.

React deserves credit for bringing fresh thinking into the JS framework space but there's a tremendous amount of room for improvement.

Re:The self-fulfilling prophecy of React by ironmagma on Thursday September 08, 2022 @11:07PM (#32774831)
So don’t memoize unless you need to. Most of the time, you don’t. This is a fairly classic case of overeager optimization. The state changes shouldn’t* particularly depend on memoizations anyway though as memoize should be mostly for view stuff (which should* be idempotent) rather than model stuff.

* = subjective and there are a few rare exceptions

Re:The self-fulfilling prophecy of React by cageface on Friday September 09, 2022 @05:27AM (#32777212)
If you want acceptable performance in a reasonably complex react app you absolutely have to memoize. The fact you think this isn’t the case suggests to me you haven’t worked on a big react app.
Re:The self-fulfilling prophecy of React by ironmagma on Friday September 09, 2022 @06:11AM (#32777439)
There’s a significant difference between having to memoize and having to memoize tons of stuff.

Generally you should only need to memoize a few things, and if you find that your app becomes brittle due to the amount of memoization you’re doing you probably are not handling state in the right way. If you’re using Redux that’s a common reason for this; don’t use Redux (is my advice). And yes, I’ve worked on several large enterprise React apps at successful startups.

Re:The self-fulfilling prophecy of React by moonchrome on Friday September 09, 2022 @06:14AM (#32777457)
> If you want acceptable performance in a reasonably complex react app you absolutely have to memoize

I think what OP was suggesting is that you don't have to memoize at every level of the tree / where it doesn't make sense.

Re:The self-fulfilling prophecy of React by danielvaughn on Thursday September 08, 2022 @09:13AM (#32765006)
Agreed. Last year, for instance, I tried Svelte. I had a great time for a while, everything was fairly simple...but then I hit a specific scenario. I can't recall what it was, but it led to me digging as deep as I could into their docs to try and understand how Svelte was doing that whole $ thing. I nearly melted my brain trying to understand it.

That's not to say hooks are much better, but I find the overall mental model to be fairly simple to understand, and I don't often find myself bumping up against React's edges.

Re:The self-fulfilling prophecy of React by apd_ on Friday September 09, 2022 @02:12AM (#32775866)
I believe you're referring to $ not being fired when another $ below it updates the variable the first $ subscribes to. This is an expected behavior. In Svelte the order of $ matters to prevent infinite loops.

In this example the second $ won't trigger the first $:

    let a = 1;
    let b = 1;
    
    $: if (a > 0) { b += 1 }

    $: if (b > 0) { a += 1 }
With React's useEffect one can easily falls into the trap of an infinite re-rendering:

    let a = 1;
    let b = 1;
    
    useEffect(() => {
        b += 1;
    }, [a]);
    
    useEffect(() => {
        a += 1;
    }, [b]);
Great framework are supposed to prevent this kind of loophole.
Re:The self-fulfilling prophecy of React by jcheng on Friday September 09, 2022 @04:00AM (#32776592)
I haven't done much playing around with Svelte so don't have any business injecting myself into this subthread, but your explanation piqued my curiosity.

https://github.com/sveltejs/svelte/issues/6730

https://github.com/sveltejs/svelte/issues/6732

So basically, reactive blocks are only allowed to run once per tick; if a dependency changes after a block has run, too bad, the block won't run again. That is really shocking to me. (I won't say more due to my inexperience with Svelte...)

Re:The self-fulfilling prophecy of React by danielvaughn on Friday September 09, 2022 @10:49AM (#32779935)
This is all triggering my memory, I had forgotten how it actually worked so couldn't explain the issue I was having. But it was related to this exact behavior. I found it so unintuitive and difficult to grasp that I literally abandoned the project and decided to build it from scratch in React.

It's a shame because it's an otherwise lovely framework.

Re:The self-fulfilling prophecy of React by addicted on Saturday September 10, 2022 @07:43AM (#32789761)
I think there’s a strong argument to be made here that the actual code in this scenario is flawed.

What Svelte is doing is simply covering up the flaws in your code by short circuiting an infinite loop scenario which has been coded into your app. This will have unexpected side effects, however.

That being said, I think useEffects is far too overloaded in React, and is currently used for completely orthogonal purposes.

At he very least it’s used for the following completely unrelated purposes.

1. Component setup and graceful dismantling. 2. Syncing with external state. 3. Running code specific to a certain prop being changed.

I think the React team would be highly justified if they created a separate hook for #3 at least. Maybe a usePropsChanged hook that is called when one of the props change, and provides an isChanged function that you can pass a prop to, to check if it’s actually changed (I’m sure someone can come up with a better design than I have in the process of writing this comment).

That would be a semantic which would actually allow you to avoid the code flaw that your code above includes, as opposed to brute force papering it over like Svelte appears to do.

Re:The self-fulfilling prophecy of React by unconed on Friday September 09, 2022 @05:56AM (#32777349)
Novice react developers reach for useEffect way too often, because they are still thinking in terms of state transitions instead of declarative states. I have spent a lot of time teaching people not to do that. They always end up very happy once they get it, because now they can build complex UIs and have a solid strategy for compartmentalization, which makes bug whack-a-mole go away.

It's not a fault in the framework, it's a fault in their understanding, where they don't know how to implement features in O(N) lines of code instead of O(N^2).

The hint is in the name: you're supposed to think in terms of formal functional Effects that mount and unmount independently and compose orthogonally.

The result will be a code base that remains maintainable even though you've kept on adding more nuanced features over months and months.

Also, your snippet doesn't cause an infinite loop at all because modifying local variables does not trigger a rerender. If you had written setA / setB, sure, it would... but I think this perfectly illustrates the explicit vs implicit distinction.

Great frameworks don't trap developers in local maxima.

Re:The self-fulfilling prophecy of React by uxcolumbo on Friday September 09, 2022 @06:43AM (#32777638)
> because they are still thinking in terms of state transitions instead of declarative states

Do you have any links where I could read more about this? Or examples?

Re:The self-fulfilling prophecy of React by madacol on Friday September 09, 2022 @10:39AM (#32779814)
I think what you are describing looks a lot like what the Svelte team is trying to achieve with the reactive assignments "$:"

A couple years ago, that feature used to work similar as useEffect, so I got used to use it for transitions, and now is biting my ass because the Svelte team are breaking my transition use-cases

Re:The self-fulfilling prophecy of React by madacol on Friday September 09, 2022 @10:03AM (#32779370)
$ has a looot of gotchas, under some conditions, the reactivity climps up the dependency tree. E.g. https://github.com/sveltejs/svelte/issues/4933

I used to use "$" like useEffect, but now I don't know how to use it anymore

And Svelte still bites my intuitions when dealing with complex objects, most of the times I've manage solve it by separating the code into multiple components, but it's not clear why that happens

Re:The self-fulfilling prophecy of React by phist_mcgee on Friday September 09, 2022 @04:01AM (#32776598)
What happens if you do want effects to update each other's dependencies, but only conditionally? eg. A certain number of cascading updates?
Re:The self-fulfilling prophecy of React by BigJono on Thursday September 08, 2022 @09:42PM (#32774325)
Hooks are much better. The only problem with hooks is that people don't get them. But once you get them, every other approach looks like a complicated pile of shit.

I think the dev community does really poorly with these kinds of abstractions that are very simple but completely unintuitive.

I place a lot of the blame on the React team too. When hooks came out, their docs were absolutely terrible. They probably still are. They were full of shitty editorialising like "we created hooks because we found classes were too difficult for people to understand!", which made every dev I worked with feel insecure for not immediately knowing how to write super awesome clean code.

Re:The self-fulfilling prophecy of React by localvoid on Thursday September 08, 2022 @11:03PM (#32774811)
> The only problem with hooks is that people don't get them. But once you get them, every other approach looks like a complicated pile of shit.

No, some people actually get them and there are way much better alternatives[1].

1. http://intelligiblebabble.com/compose-from-first-principles/

Re:The self-fulfilling prophecy of React by danielvaughn on Thursday September 08, 2022 @10:35PM (#32774641)
I'm a big fan of hooks, but I'm still not completely sold on them. I like code that reads in logical sequence, like you're telling a story. When you use `useEffect` to coordinate side effects from prop changes, it makes me feel like I'm reading a story written by Quentin Tarantino.

That being said, it's a relatively small complaint.

Re:The self-fulfilling prophecy of React by imbnwa on Friday September 09, 2022 @12:43AM (#32775391)
>I'm a big fan of hooks, but I'm still not completely sold on them. I like code that reads in logical sequence, like you're telling a story. When you use `useEffect` to coordinate side effects from prop changes, it makes me feel like I'm reading a story written by Quentin Tarantino.

I actually read this as a compliment considering Tarantino himself spoke in an interview about how his non-linear storytelling style comes from the way novels he'd read were written.

Re:The self-fulfilling prophecy of React by alex504 on Thursday September 08, 2022 @09:23PM (#32774197)
That's the thing about react, there basically are no edges or very very few edges left. There were edges in like 2018, but the team has made it relatively easy to do anything you want to do, while writing scalable and maintainable code.
Re:The self-fulfilling prophecy of React by girvo on Thursday September 08, 2022 @09:28AM (#32765203)
Agreed. I think a lot of people forgot how much pain we went through with Knockout and Angular 1 and similar magic frameworks. We seem doomed to repeat it, in some ways.
Re:The self-fulfilling prophecy of React by jmcgough on Thursday September 08, 2022 @09:08PM (#32774102)
I'm learning Vue for work and getting bad flashbacks to Angular 1. Just hoping it doesn't come with some of the terrible flaws in that framework.
Re:The self-fulfilling prophecy of React by girvo on Thursday September 08, 2022 @09:17PM (#32774151)
I can promise that it doesn’t— nothing is as bad as $digest loop hell was lol. It has its own quirks and gotchas of course, all frameworks do, but it did learn from Angular 1. That said, I’m not up to date on Vue nowadays, I was using it on its first release because we’d been hurt so bad by Angular 1 at the time.
Re:The self-fulfilling prophecy of React by neon_electro on Thursday September 08, 2022 @11:13AM (#32766772)
"this is necessary for building truly data driven apps".

So where should we try to draw the line at what is truly the kind of "app" that deserves this much JavaScript, versus a "glorified website" that management wants to see lots of interactivity/animations/etc.?

In my career, I've struggled far less with choosing a JavaScript framework for front-end interactivity, and far more with justifying how much JavaScript goes into Web experiences these days, without any concern for the end-user performance impact or other impacts that may not be as obvious, e.g. accessibility.

Re:The self-fulfilling prophecy of React by sussmannbaka on Friday September 09, 2022 @04:39AM (#32776861)
It’s worth to note that this crucial to understand thing is only ever mentioned deep inside the API reference. If it’s this essential and never once mentioned even in the advanced section of the documentation, that’s on the project and not on the developer struggling with react. They are using it as advertised.
Re:The self-fulfilling prophecy of React by sdgluck on Thursday September 08, 2022 @09:00AM (#32764853)
Here's a link to the docs for anyone else who is interested to read them: https://usegpu.live/docs/guides-memoization
Re:The self-fulfilling prophecy of React by jbreckmckye on Friday September 09, 2022 @08:14AM (#32778193)
For context, the author has only been working professionally as a front end developer for about three years - so has no such memory of $digest or ko.computed() or other such miserable kludges.
Re:The self-fulfilling prophecy of React by moltar on Thursday September 08, 2022 @03:57PM (#32771184)
Can you recommend some really in depth reading about React?
Re:The self-fulfilling prophecy of React by acemarke on Thursday September 08, 2022 @09:39PM (#32774313)
Anything particular you want to know about it?

Assuming you're asking more along the lines of "how does it work internally?", these are my usual recommendations:

- My own extensive post "A (Mostly) Complete Guide to React Rendering Behavior": https://blog.isquaredsoftware.com/2020/05/blogged-answers-a-...

- Related, "When does React render your component?", which looks more at the source level checks: https://www.zhenghao.io/posts/react-rerender

- Dan Abramov's "A Complete Guide to useEffect" https://overreacted.io/a-complete-guide-to-useeffect/

- Shawn Swyx Wang's talk "Getting Closure on React Hooks" that builds a mini version of React hooks: https://www.swyx.io/hooks/

- Rodrigo Pomber's excellent "Didact: Build a Miniature React with Hooks": https://pomb.us/build-your-own-react

Re:The self-fulfilling prophecy of React by galaxyLogic on Friday September 09, 2022 @02:27AM (#32775953)
Is it possible to explain the big idea behind React in a single paragraph?
Re:The self-fulfilling prophecy of React by eastbound on Friday September 09, 2022 @03:08AM (#32776213)
Write your code as components. The JS is nearby the corresponding HTML, and thus several teams can work on large web applications because it is so nicely componentized.
Re:The self-fulfilling prophecy of React by galaxyLogic on Saturday September 10, 2022 @02:29AM (#32788359)
A great and succinct explanation, thanks.
Re:The self-fulfilling prophecy of React by noman-land on Friday September 09, 2022 @12:22PM (#32781195)
Dan Abramov's blog is really good and this article in particular dives pretty deep into some React details.

https://overreacted.io/react-as-a-ui-runtime/

Re:The self-fulfilling prophecy of React by toddmorey on Thursday September 08, 2022 @10:37PM (#32774654)
I think your comment here perhaps proves the author's point