4 ms·
That seems like a pragmatic solution. The point of having a dev mode is to help development and catch mistakes. if(devMode) { rerender() } doesn't seem like mag
by jemmyw 2y ago
That seems like a pragmatic solution. The point of having a dev mode is to help development and catch mistakes. if(devMode) { rerender() } doesn't seem like magic. Though in general I really dislike the term magic for what is just code doing something.
- hu3 2y agoHumans have limited cognitive capacity (and time). To me magic code is code that requires a significant level of cognitive investment to understand and keep in your brain RAM. That threshold varies between people but it's always there. In React's case, the chosen solution is not the issue here. It's a symptom of bad architecture. The library should be doing more than rendering twice to protect developers from side effects, to a point that rendering twice wouldn't be required. That feels as bad as rebooting JBOSS servers once a day to "fix" OOM crashes. It's a hack at best and doesn't fix the real issue.
- edflsafoiewq 2y ago> The library should be doing more than rendering twice to protect developers from side effects Like what?
- bryanrasmussen 2y agoprobably the parent poster does not use React so it would seem a heavy demand on a human to make, given the already stated limited cognitive energy and time. I suppose I would think not rendering twice and some sort of warning about side effects would be beneficial, rendering twice means that you get a misleading view of what your application behavior is, I can see how people would - from an architectural perspective - consider it bad without necessarily using the framework.
- kaoD 2y ago> some sort of warning about side effects would be beneficial Rendering twice is exactly what allows you to detect that. How would you detect a side effect if not by calling the function twice? React's contract is that your component should be allowed to render twice without any changes so this is just upholding the invariant in dev mode.
- hu3 2y agoI do also use React, unfortunately.
- bryanrasmussen 2y agomy mistake, I feel your pain.
- hu3 2y agoIf you have to ask. Learn Elm and you'll see React's shortcomings clearer. They don't render twice but that is just the tip of the iceberg. Be warned though, exiting the cave is painful.
- jemmyw 2y agoI've used elm. It's not that great. In many ways it has the same shortcomings as react but far worse. That is you find yourself wanting to do something as yet unsupported (which we often do because otherwise why write new software in the first place) you have to break out of it's abstraction. Except doing so is 10x harder with elm, and the community around it is so much smaller you can't find the answers. Also, after I personally had moved on, there was something of a tension between the community and the authors. Maybe that's resolved, although I'd suspect for some the resolution was to no longer be part of it.
- Daishiman 2y agoThings like validating or limiting possible React code you can write such that these effects are impossible, in the same manner that abstract data types in programming languages attempt to define data structures that are correct by construction, or the same way that reactive programming enforces a programming paradigm that attempts to minimize unwanted interaction glitches.
- jemmyw 2y agoI disagree. React is saying that you need to pass it a function for rendering a component and it does not guarantee how many times that function will be called. There are documented, well defined ways to call side effects based on component lifecycles. However, how can react know what a side effect is? Hook into well known ones by hijacking the browser API? That sounds like a bad idea. Provide their own linter/compiler. That seems on the radar but I'm not sure I like the idea. Also react doesn't stop you from doing it. You can say nah it's fine I want my side effect. It just runs the render explicitly twice in development, something that could happen in production for various reasons, so you can see what will happen. Your proposed solution isn't very practical. A better solution would be a guarantee that render will only run once. But that's not how react has ever worked. There are other frameworks that do that. As with anything, there'll be a trade-off.
- CharlieDigital 2y agoIt's because the fundamental model of React's render cycle is unintuitive when combined with state. It's easy to make mistakes of this class specifically with React.
- mpweiher 2y ago> That seems like a pragmatic solution. That's not a contradiction to it being bad magic. In fact, this being a pragmatic solution is a strong, though not conclusive, indicator that the underlying abstractions are not fit for purpose. And the fact that lots of pragmatic choices led to those abstractions that are not fit for purpose also is an indicator that something about the starting point or the road is not right. Or both.
- jemmyw 2y ago> abstractions that are not fit for purpose You say that, but react is very successful, indicating that it's abstractions are working just fine for many people. Maybe we'd say they're not perfect but they fit it's purpose. It might not be my favourite frontend library to work with, but it's the one I'd be most likely to choose for commercial work because it does work well, and high use means that people have explored many solutions to problems with it.
- mpweiher 2y agoYou missed "underlying" from "underlying" abstractions. React is an implementation, not the underlying abstraction. With react it's not even clear what the underlying abstraction is. https://github.com/reactjs/react-basic/pull/12 https://github.com/reactjs/react-basic/pull/12