A mostly complete guide to React rendering behavior (2020)(blog.isquaredsoftware.com) |
A mostly complete guide to React rendering behavior (2020)(blog.isquaredsoftware.com) |
Note that I did a fairly extensive update to it in 2022 to cover React 18.
My current ongoing Redux maintenance task is trying to revamp our "Redux Essentials" tutorial to be TS-first. Making slower progress on that than I'd wanted, but hopefully can get that wrapped up in the not _too_ distant future.
Beyond that, we've got a ton of open RTK Query feature requests that I'd like to address later on this year.
React absolutely improved things from spaghetti jQuery. The zero-to-one productivity for interactive UIs goes through the roof, but growing and maintaining your project for any serious business use case is a complete nightmare.
I don't wish isometric javascript on any one. Javascript was infamously created in a week or whatever and now it's the browser standard, so be it, by why in god's name would you ever pick it for your server?
I burned myself out working on a personal project completely on my own using nextjs. I was super productive at the start, then as business logic increased... typing and flowing everything through components, state, reducers, selectors, async network calls, good God I grew to hate all of it.
I came crawling back to Rails. Everything and the kitchen sink is loaded in a controlled and blocking env. You can abuse the fuck out of it and it smiles at you. I'm sorry I left you Rails. (rails backend for all business logic: "the brain". Thin/dumb React JS client that consumes view-based APIs. React is great for interactive UIs. Not your fledgling business logic)
Web apps can reach the complexity of a desktop app with a GUI, a game, etc.
I say this as a mostly backend / systems engineer: there is an incredible wealth of complexity in the frontend. In user interaction, making and transforming API calls, and in UI/UX design.
the moment you have to keep track of any type of state, managing dozens of UI elements quickly becomes extremely tedious.
Actual React apps are as simple and elegant as a complex (web) application ever could be. Separating your app into functional components where most complexity is always local is a great way to make the application structure as simple as they can be. Where developers often go wrong, is trying to force patterns from other frameworks or languages into React and TS/JS.
I think if more people would do this exercise they would find that they don't need it, or at least that they might need some form of state reconciliation, but not enough to really warrant all the complexity the tool introduces. Some will find that they don't need it but will still use it for other reasons, like familiarity or because that's what their team uses.
If the developers have experience with other languages and native UI frameworks then they probably won't want to use any of the Javascript frameworks when developing a web application as they already have a mental model for how to setup the structure of function callbacks for handling user interactions and state changes.
This is exceedingly complex, error prone (because you might miss parts of the state that should change) and often slow (because you tend to overcorrect).
I then read it, and come out thinking "why, why, why do people put up with this absurdity?" and always stop at the same conclusion, cz they don't know any better.
I then quietly move on with my life, sometimes leaving such comments as hints to those who might catch on.
Everything in that post should still be 100% accurate and relevant.
I specifically did _not_ try to go into further details on Suspense or some of the intricacies of Concurrent Rendering (beyond "React can cancel or reset those render passes"). My overall goal was to explain the core mental model of how React's basic rendering works on the client side.
As far as Suspense goes, that can be summarized as "throw a promise while rendering, `<Suspense>` acts like a `try/catch` at the component level, React will re-render this component when the promise resolves and from its perspective that function call _always_ returned the data synchronously".
Concurrent Rendering is really complicated internally, but loosely: React has an internal notion of priorities for queued renders. `startTransition`, `useDeferredValue`, and Suspense all mark renders as low priority, so those render passes can be "rebased", interrupted, or canceled as needed based on updates that come in later.
I have https://github.com/ryansolid/mobx-jsx/?tab=readme-ov-file#mo... on my list to try since I -really- like mobx for stage management (especially mobx-keystone) and am fascinated by how clean the results can be.
Though for 'real' code I still tend to default to react + mobx-keystone because for all my gripes with react it's a pretty solid Schelling Point.
- https://react.dev/blog/2024/02/15/react-labs-what-we-have-be...
That said, React Compiler will _depend_ on React 19, because it needs a new memo/caching hook that will be included in 19.
Like, they know how to do a simple HTTP query in C/go whatever, and then don’t understand why would you need the whole complexity of spring.
It just turns out that the real world is more complex than what they assume - and they are often more than aware of that in their specific subfield.
C++ always seems to me pretty absurd just because C++ people don't seem to know better ways, and seem to think to much about squeezing out the least little bit of performance instead of aiming for simple abstractions that are performant enough. Also, the last 40 years of programming language development seems to be unknown to them, or at least doesn't influence their decisions, or if they pick up a new idea, it's implemented in the most possible awkward way (because, again, performance).
Imagine a world where you just had drawing and input handling primitives on the web. You wouldn't invent the DOM and then something like React to wrangle it that's for damn sure.
For all of Reacts faults, it's not particularly absurd to use in practice and ultimately there's only one frontend/client language to work with.
Pretty much no one that uses React _loves_ it. But we all use it because it gets the job done.
Sprinkle it a bit with some simple JavaScript/jQuery code for animations, forms checking etc, if you want. That's it. That's how it was supposed to work and that's how it works best. JavaScript was made to make the monkey dance. The end result, either made with overcomplicates frameworks like React/Angular or with simply generating the web page on the backend, is irrelevant to the user.
The majority of the complexity is in the ancillary tools and ecosystem extras, which are not strictly necessary. You don't need bundlers and transpilers, for example. The only reason they are popular is because JSX is a good convenience, though.
You can get away with very basic React for the majority of cases. Things like advanced lifecycle shenanigans are also not necessary, unless you’re doing something extra-smart, or inherently complex (due to browser APIs, third-party APIs, or just complex design patterns). But those are also not necessary: just nip complexity in the bud if possible, but if something becomes impossible to avoid, the complex side of React will have your back.
The main difference between jQuery and React-the-library is that React handles a lot of the complexity around state and component abstraction all by itself.
With jQuery the complexity and spaghetti appears much earlier than in React. It's even difficult to compare, it is much simpler to keep a React codebase cleaner than a jQuery one.
Of course, if your app is simple enough, you don't need the "extra complexity" brought in by React, and jQuery will serve you well.
But this is also true even for "simpler things" like jQuery and Ajax: if your app is simple enough, you don't need Javascript at all, you can handle 100% with server rendering. And so on.
It does require a lot of thinking before hand about the user interactions and their impact on the application state, user workflows, etc. However that thinking should already be happening regardless of the choice of implementing technology (granted it doesn't in a lot of cases :| )
Good news! We do have those primitives! The canvas element can - and is - used for drawing, and input events are fairly easy to manage. Larger projects like Google Docs or Figma often go in this direction, because for very complicated applications, it's useful to be able to handle every part of the rendering process yourself.
That said, it turns out having a rich selection of easily-debuggable, accessible components attached to a powerful layout engine is also really useful, hence why people mostly stick to the DOM unless they specifically need something that only canvas can offer.
I think this gets to the core of web devs seemingly not knowing anything else. Not knowing that, from a technology standpoint, how much better you could have it.
We can talk all day about how there are better ways to do it, but until you solve the distribution problem, the technology doesn’t matter. Mobile apps sort of solved it, but even then, the bar for installing an app is higher than the bar for clicking on a link. Plus there are cross-platform tech and policy differences to deal with, you can’t guarantee your users are running the same version of the app, etc.
writing native apps means having to have basically 4+ development teams for Windows/Mac, Android/iOS, and 5 if you want to have Linux. And it is pretty logistically difficult to herd five separate teams using the same product roadmap because the implementation of one feature can vary so wildly. But if you don't and someone gets a feature someone else doesn't get then people get upset.
I think the reactivity model React has is very compelling, and once you start thinking in terms of “GUI as a function of state,” you start trying to do the same in PHP or Swift and it just doesn’t work out as well. (Though there’s now SwiftUI which is a more reactive way to do iOS GUIs.)
I think react struck gold conceptually, and in a world where the “native sdk” (the browser) didn’t give you much, it became very compelling for many people. I think it has struggled to create intuitive APIs, and has struggled to handle real-world performance without clunky APIs.
Not to mention, browser environments face a slew of problems desktop apps don’t. You have to ship much smaller bundle sizes, no type-safe language, immature language requiring polyfills for useful language features, and the list goes on. Each problem introduces a new tool (webpack, typescript, babel, etc.), and they don’t frequently place nicely together. If I’m writing a Rust or Go program, practically every dev tool I need is part of the language toolchain. And while there are projects that aim to do that for web, they run into the “yet another standard” problem.
Backend has complex problems to solve, but at least you fully control the stack. You can use languages with simple toolchains and good libraries for writing web servers. On the front-end, every library you use is ultimately at the whim of the browser.
people also really hate feature gaps between platforms. so option one makes your customers less angry at you.
I think this is a really good thought experiment and that you are right - we wouldn't end up with the DOM if we started from scratch. It's a pretty bad local optima resulting from decades of legacy cruft. However, I do think react is closer to that ideal than the DOM.
Then you'll also have to reimplement accessibility features yourself. For a lot of use cases, that isn't worth the trade-off, imo.