Live data from Hacker News

How Does React Tell a Class from a Function?

overreacted.io

51–60 of 142 posts

Re: How Does React Tell a Class from a Function?

#51

I'm pretty much aware about prototype inheritance in js. But still scratching my head everytime, I try to understand difference between prototype and __proto__. Thankfully with ES6 classes and class inheritance constructs, we don't need to override prototype constructor and such a things anymore. Still wish, that somebody will explain it to me in plain english.

This post does try to explain it in plain English. See the explanation starting at the bolded sentence that starts "Confusingly,".

Honestly I don’t think it does a good job at explaining it. Classes are mostly syntax sugar for “old” constructors and prototypes, not the other way around (that’s the whole reason you can’t tell them apart). Would be easier to explain it that way; on mobile right now so can’t comment too much.

Re: How Does React Tell a Class from a Function?

#55
post #38

Earlier quoted context omitted.

I don't understand how "React/Vue/Angular" is considered "an entirely new language." I use React primarily, so I'm going to speak from knowledge of that. It is very obviously a framework and it does one thing - handle your view. Depending on what you are doing, you may have very little react-specific code in your application. You have data fetching, caching, persistence, authentication, tons of things, not to mention…

While I agree with your point, your example of React literally adds a DSL that requires a pre-compilation step. It's not required[0], I get that, but it's still a new language. [0]: It may not be required, but it was created for a reason....

JSX is not only not required, it's sugar that breaks referential transparency: https://medium.com/@david.chambers/jsx-violates-referential-...

Personally I still use it, but I think it's fair to say that as much as JSX was created for a reason, it's made optional for a reason too.

Re: How Does React Tell a Class from a Function?

#56

You get an error if you try to call an ES6 class as a function (without 'new'). So just try to do that inside a try-catch. If there is no error you know it is not a class and you have the result of the function already. If you do get an error and you know it is a function you know it must be a class.

I believe throwing and catching exceptions over and over as part of normal rendering is the sort of thing that would be disastrous for performance, certainly the sort of thing that the React authors would really want to avoid.

The new suspense api is based on throwing and catching promises as part of the render, so I don’t think its that.

Re: How Does React Tell a Class from a Function?

#57
post #46

Earlier quoted context omitted.

I don't mind a pre-compilation step as I'm already doing it in my projects and a lot of the community is as well. There are other templating systems you can use in React if you don't like JSX. I've seen one codebase use direct calls to ReactDOM.createElement and there is a new hotness (which I can't think of the name of now) that replaces JSX with template literals, something like: function NamePetList(props) { retur…

You're likely thinking of https://github.com/developit/htm .

Oh yes! Thank you.

Re: How Does React Tell a Class from a Function?

#60
What's the rationale for allowing the components to be defined as functions in the first place? As far as I can see, it has a single thing going for it: saving a line of code, at the expense of jumping through the JavaScript hoops to implement the class/function distinction described in the post. Besides, when the function component evolves and acquires the need for state or other features, the saved line of code goes right back in.
Post reply on HN