4 ms·
> Separate your view layer from your application state. It is actually separated, not from React though because React will need the state (data) at some point
by loh 5y ago
> Separate your view layer from your application state.
It is actually separated, not from React though because React will need the state (data) at some point anyway. We'll go into more detail on that in the next post. To touch on it briefly here, shared application state exists on its own at the top level of the app, as a composition of stateful hooks. See here: https://github.com/Molecule-dev/molecule-app/tree/6e2456e216767b529c32d16a6952e1ff0858250c/src/App/Store https://github.com/Molecule-dev/molecule-app/tree/6e2456e216...
Every possible API request also exists on its own outside of anything React (view). See here: https://github.com/Molecule-dev/molecule-app/tree/6e2456e216767b529c32d16a6952e1ff0858250c/src/API https://github.com/Molecule-dev/molecule-app/tree/6e2456e216...
Or for a more specific example, see this API resource route index: https://github.com/Molecule-dev/molecule-app/blob/6e2456e216767b529c32d16a6952e1ff0858250c/src/API/resource/thing/index.ts https://github.com/Molecule-dev/molecule-app/blob/6e2456e216...
I may be misunderstanding your complaints though. I appreciate your feedback.
- notpachet 5y agoWhat I mean is that you're violating standard separation of concerns when you have components that themselves are capable of dispatching network calls and updating the state in an async way directly within the components. As a result, those components no longer have an instantaneous view of the universe, and that makes them harder to test, harder to reason about as isolated units of abstraction, and so on. > Every possible API request also exists on its own outside of anything React That's good, but I would take it once step further and disallow any React component from directly invoking those methods. Of course you're always going to need to do something asynchronously when the user clicks a button or what have you. But in my opinion, it's a lot more maintainable to have that just be an event that the component fires, and then have something else listening for that event out-of-band (and then sending the network request / updating the state to say "connecting" or "request failed" and so on). What happens in async land is not really a pure component's business. (I know I'm definitely out of lock step with the current React ethos on this, so permit a crusty neckbeard his pet gripes.)
- loh 5y agoI understand. If you get a chance, please clone the core API and app (https://github.com/Molecule-dev https://github.com/Molecule-dev) and play around with it. Maybe you'll change your mind?
- notpachet 5y agoThat's a fair request. I'll tinker with it the next time I need to spin up a weekend project.