Live data from Hacker News

How Does React Tell a Class from a Function?

overreacted.io

131–140 of 142 posts

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

#132
post #124

On a related note, this is the one article which after reading I wanted to subscribe to, but could not find a newsletter or subscription. Dan, if you're reading this, this might help: https://www.gatsbyjs.org/packages/gatsby-plugin-mailchimp/?n... I'm guessing the site is built with gatsby from the theme.

Yup, it is gatsby. https://github.com/gaearon/overreacted.io

You can use https://overreacted.io/rss.xml for subscription.

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

#133
post #123

Earlier quoted context omitted.

You could already do that with regular functions, class inheritence, mixin, prototype inheritence, composition, event dispatching or stores. If preventing people from being confused and doing the wrong thing was the goal, I'm not sure adding one new way to the mix is going to make things clearer. Besides, while I agree tooling help with applying good practices, including encapsulation, it's no substitute for a good i…

Sure, but if you can drive hooks safely, they seems to make some things _very_ clean. I'm going back through and rewriting a toy app I haven't launched yet to use hooks and it's been a joy to work with them so far.

This seems to be the response to every addition to javascript or react without addressing the above concerns.

Nothing wrong with your comment on trying to embrace it per se, but it would be healthier if this sentiment was balanced in the community with more skepticism rather than blind embrace.

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

#134
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…

I think this in turn is a symptom of conflating rendering with state management in the first place.

The line between React and vanilla code is blurring more every day - which is a bit greedy for something that's purely meant to be a rendering solution. I would personally advocate to push React as far away from my app code as possible, and instead implement a specialised container class that hooks into React's internals (Controller? Model?) purely to handle localised state changes.

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

#136
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 guess your downvoters have some sort of Stockholm syndrome. Javascript is a giant mess and all attempts to fix it seem to make it even worse. Having a sane language in the browser would be the single biggest boost to overall productivity. That alone would push the global gdp up by 5%. At the very least.

You can’t critique Javascript. It’s one of the unspoken laws of the internet, but consistent with it reaching religious status.

The “Javascript blasphemy must be silenced” crowd is a huge reason why we have this problem. The culture reminds me of Flash 10 years ago.

It’s incredibly ironic, they’re their own worst enemies. Fortunately we now have WASM...

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

#138
post #137
post #131

Earlier quoted context omitted.

If you have to use .bind(), then no, it doesn't.

In C++ you have to use bind too.

No, not even close.

You can use it if you want to, but it has never been popular in C++, and in 20 years I have never encountered a problem that could be solved by using it, so I have never used it. And in C++ bind is part of the functional programming paradigm, as it does currying, not much to do with classes.

Bind in JavaScript makes your shoehorned collection of attributes actually behave like objects. We can see JavaScript's bind used everywhere.

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

#139
post #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.

I have developed javascript applications for a while now and I have never had to use prototype. If I was asked about it in an interview, I would explain that it's a leaky abstraction, that using it in code means you are over-complicating your solution, and that it seems to be a holdover from when people thought your codebase should be modeled according to reality ("we have users, therefore we should have a user model…

I couldn't agree more !

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

#140
post #138
post #137

Earlier quoted context omitted.

In C++ you have to use bind too.

No, not even close. You can use it if you want to, but it has never been popular in C++, and in 20 years I have never encountered a problem that could be solved by using it, so I have never used it. And in C++ bind is part of the functional programming paradigm, as it does currying, not much to do with classes. Bind in JavaScript makes your shoehorned collection of attributes actually behave like objects. We can see…

The issue we complain about in JavaScript is one like this:

    class C {
        handleEvent() {}

        someMethod() {
            onEvent(this.handleEvent); // bad; "this" lost
            onEvent(this.handleEvent.bind(this); // ok
        }
    }
C++ requires the same thing. The bad case will even generate an error at compile time.

    class C {
        public:
            void handle_event() {}

            void some_method() {
                on_event(&C::handle_event); // bad; error
                on_event(std::bind(&C::handle_event, this)); // ok
            }
    };
Post reply on HN