That said, I’m still learning React. I haven’t studied much how other people write in React beyond small, simple examples. Such examples do not illustrate how to avoid the ugliness I mean to avoid. I remain hopeful however that there are good practices in place somewhere that we can learn.
Here’s an article that looks relevant: https://dev.to/tomekbuszewski/high-level-view-and-logic-separation-in-react-39n0. However it does begin with the sentence, “One of React’s greatest strengths is the ability to separate the view from the logic.” That makes me skeptical. Assuming all known programming languages are logically equivalent, I guess you can separate view from logic in machine code. The relevant question is whether it’s easy, whether the language lends itself to that. This guy suggests you need to write two different “components” to separate the view from the logic. The fact that some React expert suggested that suggests to me that React by its nature strongly discourages separation of view from logic.
Today I have been building a small React widget that helps us estimate the daily amount of a drug in the bloodstream assuming the same dosage every day and assuming the drug has a known elimination half-life. That information is published for just about any well-known drug. The source code is here: https://github.com/xerocross/drug-steady-state . As of this writing, it is still a work in progress. I wrote a large, ungainly component file, and I don’t like that. Thus, the main thing that interests me now is to learn/develop a good reusable technique for separating things without adding too much extra fluff.
I’m starting to think that in React we have to write “components” as dumb views and move all of the domain logic into helper modules. Practically speaking, this is very similar to having two different React “components” as suggested in the article linked above. But I see a semantic difference. Having a view component and a logic component seems like it means that react’s notion of what a component is is flawed.
I have one important domain logic functions and a few helper functions it calls. I can remove that domain logic into another module easily enough. It’s astonishing how long the remaining file is, and now it’s all just React fluff and view logic. Parts of the view could be factored out into smaller components, but right now I see no value in that. And lets not forget useful code like the following.
this.handleHalfLifeChange = this.handleHalfLifeChange.bind(this); this.handleDosageChange = this.handleDosageChange.bind(this); this.increaseNumDays = this.increaseNumDays.bind(this);
I’m not sure much more can be done. Maybe React is just verbose and ugly.