136 points · by stablemap · 8y ago · 45 comments
This gives me flexibility to structure the code how I want. You can have the separate "controller" classes deal with operations (like API requests) and the "react class" mostly deals with display - it delegates actions to the controller and retrieves data from the controller. This can promote code reuse, notably certain components can share the controller class (not instance) when you have similar logic.
As a more general principle, I like react just fine as a way to express the DOM in a functional way and for its magic of figuring out just what parts of the actual DOM must be updated; but I don't really care about how React wants me to write code in other aspects, like where and how I should manage my data.
I've certainly seen a number of people in the community who insist that you _must_ keep everything in Redux, and that you should probably only use functional components along with that. I can understand why people might want to do that, as it means there's more state in the Redux store for traceability, UI output becomes highly predictable, etc. However, I would say that approach is too dogmatic and over-opinionated. It's a valid choice if you want to go there, but not something that should be enforced across the ecosystem or taught to learners. Class components, functional components, Redux store state, and React component state are all tools with specific use cases, and throwing any of them away is unnecessarily limiting your toolbox.
(Might not have been exactly what you were asking, but given how frequently this topic comes up, I wanted to put that clarification out there.)
Think of the classic case showcase of redux being used with immutable structures to implement a page-wide undo easily. There's a certain amount of state that could be burried deep in components that doesn't need to be in the global redux store.
Sure, your undo might not now be an exact representation of the historical state of the page, but the state that counts is still re-created, and that's the state that you can decide to put into redux.
Redux makes available state that is required globally, like the contents of your todo list or other properties that must persist as you navigate a web app. Components will store their own state for their specific requirements.
I suggest reading up on redux use cases to find out more before dismissing it entirely.
What is controversial is the (incorrect) notion that developers shouldn't use state. There are some perfect use cases for state and people somehow got the idea that they shouldn't use it (as opposed to not using it as the first tool, necessarily, if props can be used).
I don't necessarily recommend using this project specifically, as I don't devote any time to maintaining it. There's a whole next-generation of how I think it should work that I haven't gotten around to persuing. But I mention it to back the overall approach. It makes UI refactors extremely easy, as components are generally not responsible for any dependencies of their descendant subtree, with the exception of some parent-child interactions.
I haven't tried it, but I think Freactal [2] also seems to allow for this sort of separation of concerns.
That said, it is best used as you describe it: a store that holds all the application state and manages API requests, and then any time it updates, any components observing this store will be forceUpdated
However, instead of calling forceUpdate(), I was under the impression that componentWillReceiveProps(props) was more efficient?
Perhaps I'm wrong on that one, though... Just that forceUpdate() is an approach that is officially encouraged against.[0]
[0] - https://reactjs.org/docs/react-component.html#forceupdate
One example is the text I’m currently typing - that would only be relevant at a component level, maybe including an adjoining word counter. Once I hit reply, though, it becomes part of the application state.
An exception would be if I was continuously storing what was typed for offline use - in that case it’s always application state.
I think this blog post highlights the frustrating viral nature that is async functions:
http://journal.stuffwithstuff.com/2015/02/01/what-color-is-y...
In a world of fast CPUs, slow memory, distributed computing (whether client/server or peer-to-peer), remote function as a service, ..., asynchrony is a hard requirement.
The GUI event model has been with us since... even before the days of X11. Asynchrony in the UI is and long long has been a hard requirement. Users hate synchronous UIs, and rightly so.
Asynchrony is also difficult. But it's not like sync + threads is a panacea -- it's not because threads are way too heavy-duty. Better to deal with the pain than to deny the need.
If you just want it not to block using the page then you can just chain the sync code into a bunch of async callbacks. For actually parallelizing tasks that weren't before, it can be harder, but in my experience still not a full rewrite. It is much harder for larger applications in other contexts like the migration of Firefox to Quantum, though, yes.
Build only what you need today, and prepare for what your employer says you will need tomorrow. Anything else is like saying everybody should buy a jeep in case they move to the countryside.
Async brings its own costs. Unless your employer benefits, you are costing them money by giving them features they don’t need.
Still though, JS is a mess.
The linked blog post doesn't seem to agree with you. He goes on to talk about Promises and async/await—both features of modern javascript—which he describes as "nice":
> Async-await is nice, which is why we’re adding it to Dart
He does finish by talking about Go's concurrency model, which he describes as being more "beautiful", but I still wouldn't confuse "it's nice" for "its a mess".
Also, the line about Java doing it better seems to be a joke.
- async, continuation passing style code
- async with delineated continuations to make snippets look synchronous which aren't actually
- cooperative multi-tasking (co-routines with async I/O under the covers)
- threading (preemtible multitasking)
Each one of this choices has its problems, and all can be characterized as where on the spectrum of explicit vs. implicit state keeping you want to be. For example, with threads and co-routines you keep a lot of implicit state in the call stack, whereas with async CPS you make all of the state fully explicit. Async CPS code is the least problematic once you wrap your mind around it, but it does have "mind warp" as a hard requirement.There are times, of course, when your code is very serial, and then you really do benefit from co-routines/threads. For example, PuTTY's implementation of the SSH version exchange, key exchange, and user authentication protocols, is one HUGE C function that is actually running as a co-routine which cooperates with the UI side, with async I/O under the covers. The PuTTY approach is wonderful for that particular case: those parts of the SSH protocols are extremely serial, and the resulting serial-looking code is very very easy on the eyes (so much so that it makes up for the enormity of the function that implements it!). So it pays to be able to reach for co-routines sometimes, but not always, and I'd say not even most of the time.
Programmers today simply have to be prepared to deal with everything-is-async codebases, and they should be prepared to create codebases where everything is async from day zero.
There's nothing special about "async" per se. What about people writing OS code in the 90s? They didn't even hear about async and the "sync" was invented for the sake of easier control over the time shared between processors (remember all of our implementations of processes in OSs are CPS-style, where they just give up the control over their stack if run for more than X ms).
Other thing I do recently consider a lot is asynchronicity between machines: 1) receive a request 2) send a database request 3) return an error if database request had timed out ...) etc. Having a language that spans several systems, enveloping time, execution redundancy contraints and ensuring data passed is not garbage is something that would be novel.