News for hackers. Stuff that matters.
Re:Why React Re-Renders by synu on Tuesday August 16, 2022 @02:40PM (#32486933)
If you don’t mind a slight tangent, where would you start today to learn front end development with React such that you learn this sort of thing as you go?
Re:Why React Re-Renders by lhnz on Tuesday August 16, 2022 @03:16PM (#32487447)
The new version of the official docs is currently in Beta and this is really, really good: https://beta.reactjs.org/learn

(In fact, I'll go further than this and add that the section on "Escape Hatches" should be re-read by senior/lead engineers, as many have misconceptions due to learning concepts ad-hoc from code of mixed quality: https://beta.reactjs.org/learn/escape-hatches)

Re:Why React Re-Renders by 9935c101ab17a66 on Wednesday August 17, 2022 @03:10AM (#32493211)
Hmm. The escape hatches page is just “coming soon” for me?
Re:Why React Re-Renders by francislavoie on Wednesday August 17, 2022 @03:16AM (#32493256)
That page is just the "category header" for a bunch of other pages. Look at the sidebar (or on mobile, hamburger menu) to see the actual content.
Re:Why React Re-Renders by jasim on Tuesday August 16, 2022 @02:56PM (#32487169)
This is diffused cultural knowledge, but most of the ideas in React can be understood as: view = render(state). It re-renders everything all the time. But practical considerations force some optimizations. Everything else in React follows from those optimizations.

For example, if you destroy and re-create DOM all the time, then it loses critical information like user's cursor position and text selections. It is also slow to read and write to the DOM. Thus the need for an intermediate data structure, the virtual DOM.

React re-renders the virtual DOM every time the state changes. And it then diffs the previous and current ones against each other and does just the minimal number of DOM mutations to sync it up. To speed this up a bit, array elements are denoted with "key" so (I assume) there is a way to see if an element has been added or deleted.

But re-rendering the virtual DOM all the time can also be costly in terms of performance. Thus the next set of optimizations: React.memo+immutable data, and so on..

Re:Why React Re-Renders by Tiktaalik on Tuesday August 16, 2022 @05:18PM (#32489085)
I've found myself needing to learn React on the go while working on an evolving React codebase, and needing to improve performance significantly and it's been very hard because casually googling for how to do things in React yields lousy hits full of extremely high level "babys first react todo app/blog" type articles.

Absolutely not what I need.

What I want to know is how to build things with React as performant as possible. This discussion of complex and performant apps seems elusive.

Re:Why React Re-Renders by ng12 on Tuesday August 16, 2022 @03:03PM (#32487272)
The idea is you shouldn't really need to. You have tools to memoize expensive operations in a React-friendly way, but even then you shouldn't really have to think about render cycles.

YMMV but I the only time I've ever had to really think about this stuff was when trying to frankenstein legacy jQuery code into a React app.

Re:Why React Re-Renders by RussianCow on Tuesday August 16, 2022 @03:16PM (#32487453)
In my experience, it's pretty easy to hit performance bottlenecks in React, so I don't think it's that uncommon to have to dive deeper for any reasonably complex codebase. Also, you basically have to understand the React render cycle to effectively use `useEffect` for anything more complex than "do this thing on mount".
Re:Why React Re-Renders by synu on Wednesday August 17, 2022 @01:39AM (#32492750)
I get that, but I personally enjoy understanding the full stack. I built a cpu on breadboard from kit for fun, its just something I enjoy doing.
Re:Why React Re-Renders by azangru on Tuesday August 16, 2022 @02:45PM (#32487009)
If you are starting to learn front-end development today, you may question the choice of React.

Consider that a decade ago, people who were starting with the frontend were learning jQuery, which is almost irrelevant now.

Re:Why React Re-Renders by ramesh31 on Tuesday August 16, 2022 @03:02PM (#32487251)
>Consider that a decade ago, people who were starting with the frontend were learning jQuery, which is almost irrelevant now.

It's not that simple. At the time, jQuery was absolutely pivotal in bringing about ES5 and the transpilation revolution on the front end. More or less its' entire API was subsumed by the browsers, and so jQuery became unnecessary, but far from irrelevant.

I can see the same thing happening today with React/JSX. There is simply no better way of expressing a UI than JSX-like components. And FRP as a paradigm for UI development is here to stay. So the future of web dev probably looks something like Yew [0].

[0] https://yew.rs/docs/getting-started/build-a-sample-app

Re:Why React Re-Renders by nfRfqX5n on Tuesday August 16, 2022 @05:44PM (#32489379)
If you want a job, learn react
Re:Why React Re-Renders by outworlder on Tuesday August 16, 2022 @03:05PM (#32487299)
What would they be using instead?
Re:Why React Re-Renders by azangru on Tuesday August 16, 2022 @03:11PM (#32487372)
They should probably start with native browser apis for DOM manipulation, and web components. And then explore the current landscape for options that alleviate the pain points discovered while learning (e.g. Lit is nice for declarative reactive components). All the while being conscious of the tradeoffs.
Re:Why React Re-Renders by 411111111111111 on Tuesday August 16, 2022 @03:23PM (#32487547)
That's a fine idea if you're not expecting the team to grow beyond the original author(s).

It's a pretty terrible idea if you're going to do it in a business setting, as the original author(s) will always be it's Achilles tendon, making the project a liability before it even goes into production.

Most projects use react or angular because it makes onboarding new members easier, and these frameworks really aren't as bad as some people on hn claim.

Re:Why React Re-Renders by azangru on Tuesday August 16, 2022 @03:38PM (#32487747)
How much have you researched this space? Do you know of companies that are successfully building their products with web components? Hint: these would include Adobe, Microsoft, RedHat, and GitHub. How do they onboard new members to their terrible setup, one might wonder?
Re:Why React Re-Renders by RussianCow on Tuesday August 16, 2022 @04:35PM (#32488543)
> How do they onboard new members to their terrible setup, one might wonder?

I can't speak for the specific companies you listed, but the number of times I've heard someone sing praises about a homegrown UI framework at their place of employment is approximately zero. The sentiment expressed about those is generally hatred and agony.

That's not to say it can't be done well, but most don't. Also, a company being able to hire/onboard engineers does not imply that their onboarding process is smooth, or that their house-made framework is well designed.

Re:Why React Re-Renders by azangru on Tuesday August 16, 2022 @05:03PM (#32488891)
> a homegrown UI framework

I am deeply puzzled by both your and the sibling comment, which suggest that the only way to go is to build a framework. To advance such argument, especially when comparing something to React, is to forget that:

    - React also for a long time was advertised as a view-layer library for creating UI components, not as a "framework".
    - There've been numerous debates in which advocates of Angular or Ember were suggesting that because of such inherent lack of structure, React apps were always different between projects, as opposed to the clear conventions used in Angular or Ember project. This did not deter React supporters and did not prevent React from succeeding.
    - React was created as a library when web browsers did not have a standardized component model; just as jQuery was created as a library when browsers did not have a unified way of interacting with the DOM. Years have passed, and web browsers have matured to the point when a native component model has become a reality. You do not need to home-grow a framework in order to take advantage of them.
Re:Why React Re-Renders by ReadTheLicense on Tuesday August 16, 2022 @06:22PM (#32489752)
Problem is, Web Components still don't support reactivity and passing complex props, so you need a framework anyways, and at that point it might as well be React.

I've seen people do abominations like each web component is a React root, message passing systems on the side for complex objects... better use React directly

Re:Why React Re-Renders by RussianCow on Wednesday August 17, 2022 @03:03AM (#32493165)
> Years have passed, and web browsers have matured to the point when a native component model has become a reality.

This is not true at all, at least not enough to be able to completely replace React (or any frontend framework) with native APIs. You still need some sort of high-level abstraction(s) to tie everything together.

Re:Why React Re-Renders by nwienert on Tuesday August 16, 2022 @06:24PM (#32489771)
Was going to say the same as sibling @ReadTheLicense, but further Web Components were started before React so the idea that they are more modern isn't true.
Re:Why React Re-Renders by 411111111111111 on Tuesday August 16, 2022 @03:41PM (#32487791)
Sure, if that's the scale of your project right from the start then creating a new framework from scratch is always an option.

That's the origin of both react(Facebook) and angular(google) after all.

Re:Why React Re-Renders by cercatrova on Tuesday August 16, 2022 @03:42PM (#32487794)
The phrase is Achilles' heel for which the tendon is named.
Re:Why React Re-Renders by gherkinnn on Tuesday August 16, 2022 @06:06PM (#32489578)
It does help to have used the basics to understand the benefits of a full framework. But learning things bottom-up isn't suitable for everybody.

As for web components, I'd rather not. Nothing about it works for me. Not the class-based decorator syntax. Not the CSS scoping. Not the use of custom elements. The prop syntax is awful and the paradigm just feels cumbersome.

I work with Angular (its component model is close enough) and have read the Lit docs. Never will I choose that option.

A function is just a better way to write UIs.

Re:Why React Re-Renders by gregsadetsky on Tuesday August 16, 2022 @04:34PM (#32488528)
Scrimba has pretty great interactive classes too. They specifically have a free React one: https://scrimba.com/allcourses?topic=react

It's quite high quality and the learning environment is great: you're in a live code editor + hear and see the teacher's code/cursor movements.

Re:Why React Re-Renders by RyanHamilton on Tuesday August 16, 2022 @03:23PM (#32487542)
I can recommend React with Mosh: https://codewithmosh.com/p/mastering-react I completed it and together with the react official documentation (particularly on hooks and newer react 18 features) have learnt enough to build a good interactive application. Building the app immediately after has taught me much more. You can try patching free tutorials together but given the salary paid to good developers, paying for good training is a great investment.

I chose react as I had a large application to make and I knew react had 2 critical libraries I wanted to reuse. Having now learnt react and the underlying ideas, I think Svelte (https://svelte.dev/) may solve the general problem better. However it has less libraries/documentation and community support. If I had more time to learn I would have considered learning it as a potentially superior solution.

Re:Why React Re-Renders by droidist2 on Tuesday August 16, 2022 @10:12PM (#32491499)
What aspects of Svelte would have made it more suitable?