Live data from Hacker News

How Does React Tell a Class from a Function?

overreacted.io

71–80 of 142 posts

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

#71
post #29
post #20

Earlier quoted context omitted.

You don't write js because you like, is because is a competitive advantage: - Write in one language only for both client and server side. Why is good? You just need js devs, no need to duplicate validation and other logic. I'm making a game right now and has js on the backend. I want to make the game single player, now I can just import a file from the client side and > now is single player. If your backend is in jav…

I really don't get how JS on the client and the server is any real advantage - besides sharing some syntax, the models and paradigms are so drastically different it might as well be two different languages. Why make the server so bad when you don't have to? I think JS has its place despite it's flaws, but for the sake of sanity I can't imagine ever using it on the backend by choice. If you want the ease of express, u…

My code is barely coupled with any server engine. My code is just agnostic. Everything is a function. Then I put some wrapper to connect my function to express API or sockets. My code probably is not aware is running on the client or server, nor should be aware of that.

For me works amazingly. I couldn't see myself using anything else on server.

It's not hard to pick up a new language, but changing every 5 minutes it is, especially when you create a new feature.

I think I have 75% of the code shared between client and server side. Again, I can't see myself duplicating so much code and testing it.

I use typescript by the way, so it's pretty good imo.

Just think of all the utils that are shared between client and server side.

Of course I'm talking about web applications with streaming data, not simple webpages.

In a project before we had some functionally that was done on the client side, then somebody decided to move on the server side. Has to be written again in Java from scratch. It's doable, but it's more expensive.

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

#72
post #64

Earlier quoted context omitted.

To me it's all about intent. By using a function instead of a class, you are basically saying "future reader, this component is nothing more than a simple mapping from those values to that DOM tree, don't try to look for internal states or any complex treatment here". Sure you could write a "normal React component without state" and do the same, but having those properties baked in the way you defined the component i…

I absolutely agree with you. This makes me very skeptical about the proposed feature in React, called "hooks", which is linked to in the article. It lets you add state to your functional components, and it looks like you end up bundling all your code into one function rather than having a separate constructor to initialise the state (to me, separating out that function is a good thing). I would be interested in the m…

>to me, separating out that function is a good thing

To us, too, and that's exactly the point of Hooks. They let you extract logic into functions in a way that wasn't possible before. I suggest to read more than a single page -- in particular, extracting custom Hooks is largely the point of the proposal.

https://reactjs.org/docs/hooks-custom.html

I also wrote about this here:

https://medium.com/@dan_abramov/making-sense-of-react-hooks-...

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

#73
> I didn’t know this for years. Please don’t turn this into an interview question. In fact, this post is more about JavaScript than it is about React.

Well, I think how prototype works is much more important than using React.

I think having an interview in JS without knowing prototype is like having an Java interview without knowing polymorphism, abstract class, interface or something.

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

#74

Earlier quoted context omitted.

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.

I tried not to mention classes too explicitly in the section that deals with __proto__, and only explain the prototypes themselves. But keep in mind that new readers are often exposed to classes only so they need some practical ground. In any case there's plenty of resources -- maybe my explanation will click with somebody.

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

#75
post #56

Earlier quoted context omitted.

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.

Suspense only uses throwing when it's blocked on network. There is huge difference between doing this once every few seconds, and doing this thousands of times per second.

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

#76

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.

That would unfortunately be too slow for production.

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

#77
post #68
post #53

Ugh. This just doesn’t make me want to touch JavaScript. I’d rather stick to Django and Python

I'm quite sure the authors of Django would be able to tell similar stories about Python...

Yeah that's kind of the point of this blog. I dive a bit deeper than you need to know as an app developer. Also touched on this at the end:

>If the final API is successful, its users never have to think about this process. Instead they can focus on creating apps.

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

#78
post #17

I have written maybe 200 lines of JavaScript in my life, and this article has strongly reinforced my desire to never have to write a single one more. The next time someone asks me what the problem with JavaScript is, I will send them this article. "Scheme for the browser" what a joke. Some excerpts for those that didn't have the strength to sit through the entire thing without pulling their hair out: > If you called…

Author here :-) I don't think I disagree with you. JS has its warts and some of them are... weird. The web delivery mechanism has always been its killer feature, not the language. And for web delivery mechanism to work, it needs to maintain backwards compatibility.

In practice, however, I think a lot of these things become irrelevant because JS subcommunities carve out reasonable subsets of the language. For example when using React you almost never need to care about prototypal (or even normal) inheritance because it's explicitly discouraged by React. And in the future, we might not need to encourage classes when writing components altogether.

If you limit yourself to avoiding some features then it's not such a bad language after all.

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

#79
post #17

I have written maybe 200 lines of JavaScript in my life, and this article has strongly reinforced my desire to never have to write a single one more. The next time someone asks me what the problem with JavaScript is, I will send them this article. "Scheme for the browser" what a joke. Some excerpts for those that didn't have the strength to sit through the entire thing without pulling their hair out: > If you called…

I'd say JavaScript works well enough for its original goal - simple DOM manipulation in response to UI events. IMHO the trouble comes from folks who want to use it for all kinds of over-reaching frameworks. The moment you have to discuss enumerating properties of classes, prototypes, etc., JavaScript's lack of typing becomes painfully obvious and there are just much better languages to spend your time on.

I also like the original practical vision for node.js/CommonJS as an escape from the metaprogramming excess that has become Java.

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

#80
post #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 goe…

> What's the rationale for allowing the components to be defined as functions in the first place?

Intent: it makes clear that the component is stateless in a way a class-based component does not.

Post reply on HN