by jeswin · 5y ago · 0 comments
But there is one idea from React which made a ton of sense; from the time I started using it nearly seven years back. That's JSX - instead of shoehorning expressions into markup, move markup into the programming language. Forgo embraces JSX and skips most of the other ideas in React in favor of a more plain vanilla approach.
What should a developer do that needs to share state across a Forgo app?
It would be awesome if browsers just had a real template language like JSX, we'd see more "web components" which are just a little bit of JSX/or whatever templates and some DOM APIs. Developers are tired of managing binding and unbinding events. React does that well. I assume Fargo does too.
So putting aside the redux/flux paradigm, what else is the real hurdle being solved?
We already have many facilities in JS to handle state. Singleton patterns, closures etc. The example on the website uses closures to handle component state - which is simpler than React's hooks based approach.
function SimpleTimer() {
let seconds = 0; // Just a plain variable
return {
render(props, args) {
setTimeout(() => {
seconds++;
rerender(args.element); // rerender!
}, 1000);
return <div>{seconds} secs have elapsed...</div>;
},
};
}
> I don't consider reducers, actions, redux/flux to be React specificThey didn't emerge from React - but it isn't possible to do React without them. When I conduct trainings, I often see some of my students go - wow, why would you do that? So Forgo basically is an exploration of how to simplify things - and to let users pick their desired level of complexity.
Maybe you mean it's not realistic to write React "beyond toy examples" or "in the real world" without Redux, but still, I'd beg to differ there. You can get very far with plain React and the context API and other libraries. Redux definitely isn't required.
Forgo does look cool though! I like the idea of using plain variables for state, and returning an object allows you to add other things to that object (didMount, didUpdate, unmount etc).
That said, this is a cool framework, congrats on completing it! I'm also working on a minimalist React-ish framework though in a very different approach.
It is 100% possible to build large React apps without redux/flux. There are entire ecosystems built around, say Mobx, among many other libraries and tools which don't even follow the flux style patterns.
> The example on the website uses closures to handle component state
I'm not asking about component state; I'm asking how do multiple components access app-wide state. I can't use a singleton for that; how do I know when that singleton is updated? I guess I could use a proxy but I don't want to do that.
> and to let users pick their desired level of complexity.
React is the view layer. It doesn't enforce any type of data layer or state management layer.
It's up to the developer to make that decision. An AccountInfo control could be listening to "ACCOUNT_INFO_CHANGE" events (emitted when account info changes), and can subsequently rerender and read off some singleton state. Rerenders are explicit.
And like you said, proxies could be another option.
Here is a React/WebGL game I made which tracks a lot of variables and state, but does not use redux/flux...because it's 100% possible to make even make a 3D game with React without them!!!
This looks a lot like closure components from Mithril.js[0], which have been around for several years. It's nice to see people independently come to the conclusion that this approach is simpler :)
[0] https://mithril.js.org/components.html#closure-component-sta...
I've created many React apps without any complex state management libraries :-)
It's plain JS with a very similar DX to JSX, and I think addresses that really well.
My team also works closely with Chrome, Safari, and Firefox on proposals like Template Instantiation that aim to build up more APIs that template libraries can use to be smaller, faster, and more interoperable.
I really think we will eventually get good templating in the DOM so at least the most common use-cases are taken care of.
HTML imports were supposed to be the holy grail to solve this, but nobody but Chrome supported it :(
- return values are a data structure, so they’re render-target/render-implementation agnostic
- it’s compile time only/effectively a macro, so the compiler can choose how to render subtrees
I love the “it’s actually part of the language not a build tool” part of lit, but it can’t satisfy those other goals without giving up its core selling points.
JSX actually doesn't return a data structure. It's syntax only and some transforms do things quite different from what React.createElement() does. And this is part of the problem with it and why it can't really be standardized - there's no semantics to standardize.
lit-html as an render implementation does indeed target only the DOM, it's not trying to target non-web platforms.
const [v, setV] = useState(123);
First call v gets set to 123, second call it doesn't. WAT!Algebraic Effects are the thing I'm most glad React introduced, hooks are nothing short of a revolution for UI.
Everything that IS actually just JS is listed (hooks, actions, reducers), but the only thing that isnt (JSX) is used without a question.
Sometimes React critique just reads like an elaborate trolling attempt.
“It’s just JS” can certainly do that but you need additional compile time signals.
We've been trying to remove view from logic since the 90s, and move towards declarative views with data binding. Keep your nook and crannies away from the presentation layer.
But all the industry wants is shoving PHP in the browser.
I think it's sad. We're making the UI layer always more complicated.
Let's say your UI changes, like a redesign... you can rewrite your component and probably need to change most of them.
If your UI was just dumb and declarative and the data was salt on top, changing the UI is trivial.
The UI should declare what the UI should look like, not describe how it should do it.
This is why, as an old timer, I much prefer the direction VueJS took.
Edit: great lib though, anything small is a win.
The JS has to grab DOM nodes by class or ID and is highly coupled to its structure anyway, so putting them together (as React does) seems far more logical in my opinion. Separating them into files and using string classes to look them up is far less easier to read and leads to runtime issues (since a typo isn't actually a code error but just silently doesn't work).
JSX is really not like PHP at all. PHP just writes out strings - JSX is all JavaScript objects. What you are describing is more akin to something like Handlebars or Angular templates. Plus, JSX is just sugar and is not required to use React if you prefer something like hyperscript (no compile step needed in that case).
My point mostly was: separate view from logic. Intertwining them only makes maintainability and cognitive juggling worse.
This is exactly what React does especially for stateless components that purely consume data. The component declartively says this is the data I need. The component doesn't care how that data is retrieved, that's an implementation detail separate from React.
For stateful components, this is also achievable but requires a little engineering and some good architecture (this is IMO the hardest part of 'React' - and this isn't even a concern of React - since React doesn't prescribe any specific approach).
If you define all the functionality for a component well, you should be able to easily rewrite just the view (the return value of a fucntional component or of the render method for class components) without changing any of the existing functionality.
This can be done by defining your component's behaviors via hooks (custom or builtin). Those hooks should provide any functionality and data your view needs and exposes that to the component. If no new data or functionality is needed, then nothing in these hooks (and more broadly code external to the render/return value) changes.
All that changes is the return value of your component and any css you want.
If you change the view, yeah you might have to update some event handlers for buttons that are now inputs, but that's not a UI only change, thats changing functionality too.
The worst PHP code I’ve seen is the kind that mixed database queries with presentation, and that was just bonkers. Just a really truly awful mess.
For me, that was the “PHP” I envisioned and what I didn’t want to rehash with React, and I can confidently say that has worked out pretty well.
I prefer the template syntax of .vue files or similar to the one riot.js uses. They use basically the same syntax html uses, just on a smaller scale and limited to a single component. That way you don't have to invent new was of embedding markup in code.