3 ms·
> I just want to point out how fundamentally flawed this is. The constructor is meant to initialize internal and initial state of objects. Making a data/network
by AWebOfBrown 9y ago
> I just want to point out how fundamentally flawed this is. The constructor is meant to initialize internal and initial state of objects. Making a data/network request in the constructor is beyond bizarre. In fact, the general consensus was that doing that sort of thing in the constructor is kind of an antipattern.
Note that he said synchronous data fetching, ie a local cache. If you are querying a local cache, this enables you to grab data before the first render (saving a re-render) which is more obviously beneficial on low-power, slow-to-render devices.
It CAN also be used to initiate a network request, but this is discouraged because it can fire multiple times: meaning you should only do this if the endpoint is idempotent and you can live with excess requests. It may also complicate testing.
Bit OT, but I generally think "that's an anti-pattern" criticism isn't a great contribution unless you're specifying what the problem is.
- dvt 9y agoMy criticism was in the context of SSR (in other words, there is no "local cache"). > Bit OT, but I generally think "that's an anti-pattern" criticism isn't a great contribution unless you're specifying what the problem is. I thought I was pretty clear: putting that kind of thing in the constructor is terrible practice and goes against common sense.
- AWebOfBrown 9y agoI mean detailing specifically what problems arise. To my mind, calling something a 'terrible practice ... against common sense', particularly when another party (with expertise no less) disagrees, is just a lazy put-down.
- dvt 9y agoI can be clearer if you need me to, but I think this is very common knowledge: the advice of "stick it in the constructor" abuses constructors and makes them do something they weren't meant to do. Incidentally, abusing constructors also sometimes breaks testing. Constructors (in virtually all languages) are notoriously difficult to test as is[1]. [1] https://stackoverflow.com/questions/14569499/javascript-mocking-constructor-using-sinon https://stackoverflow.com/questions/14569499/javascript-mock...
- davedx 9y agoYup... don't have side effects in constructors. That's the whole point of other lifecycle methods... this is a practice as old as C++
- danabramov 9y agoPlease note we are not suggesting side effects in constructors. I’m sorry if my previous comments gave you that impression. I elaborated here: https://news.ycombinator.com/item?id=16696836 https://news.ycombinator.com/item?id=16696836
- davedx 9y agoThanks for the clarification and for pitching in on this thread. FWIW I really appreciate the thought and effort the React team puts into changes.
- coldtea 9y ago>My criticism was in the context of SSR (in other words, there is no "local cache") Why would there be "no local cache" in the context of SSR? >I thought I was pretty clear: putting that kind of thing in the constructor is terrible practice and goes against common sense. Those are adjectives, the parent asked for technical arguments.