by vbezhenar · 2y ago · 0 comments
1. They introduced hooks which must be called in the same order for every execution. You can't put hook inside `if`. Hooks are based on magic. This is terrible design.
2. They didn't adopt async/await, inventing their own suspend stuff.
3. Now this: they just convert the source language into something else.
The JSX idea is gold: introduce XML-like template syntax into the language to avoid need for foreign template language. Make XML templates valid at startup and even with proper types for TypeScript. This is very important advancement compared to text templates which are used everywhere else.
The stateless render function is good. Emit desired-state, let framework to reconcile current state to desired state.
But rest of React is not gold.
Async and await is not useful for React, Suspense is done out of necessity. It's have to be generators if anything - but Suspense is older than wide support of generators and async/await too.
You don't have to use hooks nor Suspense at all - just use your own state management and pass it as a prop at the root render call, or no state at all.
I think it’s reasonable to wonder why they’re implemented in such a strange and brittle manner as that. Vue needs no such weirdness, the composition api just uses normal scoping in JS.
Your comment might not bring much new information, but I agree with what you say.
Except maybe for one thing: "state" is about observability. That's what justifies the "rules of hooks", not the inplementation details.
I guess people call hooks magic because they look simpler than they are, as if their implementation would rely primarily on closures, which is not the case.
But they play very well with reasoning about closure scoped inside of React components.
Regarding async/await, I'm really pretty much torn. Why can't Suspense wait for any Promise to resolve? Or has this changed by now?
Regarding conditional hook calls: this is really a non-issue in practise, it sometimes even helps with code quality.
Would you initialize an observable property of an object/component conditionally?
I think I'd prefer to avoid that as well.
But, React implements the core component tree rendering logic via a single `while` loop that iterates downwards. That's flat, logic-wise, whereas the tree is nested.
Meanwhile, React already had similar behavior for its error boundaries, where a thrown error in a component would get caught by the component rendering logic and it would "bubble up" to the nearest error boundary.
So, they opted to implement Suspense's mechanics the same way, except that instead of throwing an error, you throw a `Promise`.
Meanwhile, React components on the client have always been pure synchronous functions. No `async/await`, no generators, and thus no support for returning a promise from a function.
With React Server Components, React now supports `async function` components _on the server only_. They've done some prototyping with support async components on the client (and I think even briefly accidentally had a couple releases where that technically was turned on), but there's some kind of either technical issue or release planning issue that's kept them from building out that support for client components (possibly support for `AsyncContext` in browsers).
What I don't understand is why there's no easy way to conditionally render a subtree depending on promises contained in the props of the top-level component.
Kind of like a guardian HOC triggering a re-render whenever all props promises resolve.
Is it because that would be a one-time thing and not synchronously reactive?
Seems like people reimplement or reuse this kind of thing all the time, but often using useEffect (transforming side-effects into state, losing deterministic rendering).
You're right that this would get ugly really fast with nested suspense boundaries though.
Once you learn the basics, as long as you stick to them you can write code so fast / solid.
I haven't bothered with anything beyond hooks, so maybe that's why I'm content.
In a React-based project, I managed to use RxJS through react-rxjs[0] to manage events and state. Thanks to it, I was able to fully streamline state management in that project.
React does let you do a lot in async, when you are managing the rendering of components something like Suspense could be the right idea but I have a very big application that breaks some of the rules of React but gets away with it and I don’t know how it will do in React 18.
It’s really nice. It started in php as xhp in the late 2000s and was later ported to JavaScript as jsx.
The worst part of react is all the FUD hacker news creates about it every time they try to innovate to make the web's defacto framework better.
which is completely dependent on a compiler since it bakes in all of the updating logic at that stage. I haven't done big projects with it but for little projects I have been amazed at the speed and the small size of the bundles.
Good. JavaScript sucks.