Live data from Hacker News

How Does React Tell a Class from a Function?

overreacted.io

111–120 of 142 posts

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

#111

Earlier quoted context omitted.

I have this doubt as well. Hooks will allow people to use "class like" features and behavior, but with none of the conventions forced by syntax, plus some pitfalls. To me it seems a good way to ensure most devs (espacially unexperienced casual ones, which are legions in JS by nature of the market) will dump their logic wherever it works with as little structure as it allows. It's a technical debt catalyser, and will…

> will dump their logic wherever it works with as little structure as it allows. It's a technical debt catalyser, and will make sure nobody feels at home in someone else's code. The community will pay the price in a year. I don't want to downplay your concerns, but I will say that this is already a problem. Part of the goal with hooks (and I can't say if it achieves that) is to encourage the separation of a lot of st…

I read the hooks documentation. Why would someone find this pattern better than a pre-defined state object ? I don’t get it.

React seems like it’s getting more complicated every single day. I loved it due to its simplicity but now i’m not so sure.

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

#112

Earlier quoted context omitted.

> I really don't get how JS on the client and the server is any real advantage from my own experience, using single language has huge influence on overall code quality. Makes you understand it much better, comparing to situation you must switch between multiple languages. As you gain more experience in it, the less obvious bugs you'll make (just by knowing what's good and what's wrong). The worst js code I see, is co…

Can I ask if you've ever programmed in another language on the backend? I've done plenty of JS and many backend languages and I'd never consider using JS on the backend. > from my own experience, using single language has huge influence on overall code quality. From my experience, JS code is really poor quality and overly complex (looking at your build chain) even when written by an expert.

i could argue with you here, but i rather skip

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

#113
post #111

Earlier quoted context omitted.

> will dump their logic wherever it works with as little structure as it allows. It's a technical debt catalyser, and will make sure nobody feels at home in someone else's code. The community will pay the price in a year. I don't want to downplay your concerns, but I will say that this is already a problem. Part of the goal with hooks (and I can't say if it achieves that) is to encourage the separation of a lot of st…

I read the hooks documentation. Why would someone find this pattern better than a pre-defined state object ? I don’t get it. React seems like it’s getting more complicated every single day. I loved it due to its simplicity but now i’m not so sure.

mostly because you can encapsulate and reuse some state logic. allows for easier testing too.

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

#114

Earlier quoted context omitted.

I have this doubt as well. Hooks will allow people to use "class like" features and behavior, but with none of the conventions forced by syntax, plus some pitfalls. To me it seems a good way to ensure most devs (espacially unexperienced casual ones, which are legions in JS by nature of the market) will dump their logic wherever it works with as little structure as it allows. It's a technical debt catalyser, and will…

> will dump their logic wherever it works with as little structure as it allows. It's a technical debt catalyser, and will make sure nobody feels at home in someone else's code. The community will pay the price in a year. I don't want to downplay your concerns, but I will say that this is already a problem. Part of the goal with hooks (and I can't say if it achieves that) is to encourage the separation of a lot of st…

> is to encourage the separation of a lot of state-interaction OUT of the components

I get that, but I'm not sure it's in the interest of most projects either. The vast majority of React code base can (and IMO should) do the simplest things instead of going with a store. Redux and other means of putting state interactions out of components are a huge overhead in so many ways that I think it should be the exception, not the rule.

Besides, even for fluent react devs, it is a common strategy to create applications as a big fat component that does it all, then split it little by little into smaller parts as the need arises. Complexity management is hard after all.

It makes sense the FB team wants to promote a pure clean industrial flux pattern, so I understand why they go this way. I just note they are doing so, ignoring the fact the vast majority of devs work on code that never reaches anything close to their requirements. And if it ever does, it certainly doesn't start that way.

Same goes for tests. I'd say only one projects out of give I work on are decently tested. Our industry has low quality standards it seems. But creating something that will cause confusion among the less skilled in hoping it will help the experts do fewer mistakes is not a good trade off: IRL teams have one 10x programmer for many more regular ones, if at all.

Again, I follow the logic. I just think it's not a good bet for the community. It may be a good bet for FB scale entities, but I'm not even sure it is.

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

#115
post #64
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…

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…

How do hooks fit in with that definition?

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

#116

Earlier quoted context omitted.

>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-se…

That post has changed my opinion about hooks. I find this gist especially compelling: https://gist.github.com/gaearon/cb5add26336003ed8c0004c4ba82... Without hooks, that example would require adding a method (or two) to the component class. With custom hooks, that code can go into a separate module. Component bloat has been a problem for us, and if hooks can enable components to focus solely on render logic, I'd say…

Can't you achieve the same result by leaving only view logic in the component, and having the data-related logic, including the fact that data has changed in a way that may require redraw, handled in a separate object? Here's how I'd do it in Mithril/TSX:

    class WidthStore {
        width: stream.Stream;
        constructor() {
            this.width = stream(window.innerWidth);
            window.addEventListener('resize', () => {
                this.width(window.innerWidth);
                m.redraw();
            });
        }
    }

    const widthStore: WidthStore = new WidthStore();

    class MyResponsiveComponent {
        view(vnode) {
            return 

Window width is {widthStore.width()}

; } }

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

#117
post #111

Earlier quoted context omitted.

I read the hooks documentation. Why would someone find this pattern better than a pre-defined state object ? I don’t get it. React seems like it’s getting more complicated every single day. I loved it due to its simplicity but now i’m not so sure.

mostly because you can encapsulate and reuse some state logic. allows for easier testing too.

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 initial API design.

React and JS API are confusing, because they have been designed this way from the start. We are now adding things on top of it, again and again, without ever fixing anything at the bottom. This creates problems at the same time it solves others.

Another issue is that the JS community wants to professionalise without aknowledging that a huge part of it doesn't have the skill to do so yet. It's a community that includes many young devs with little experience, people that are not programmers but ends up doing so, devs born in a culture of instant results... It's important to address this as well, by talking to them in tutorials and documentation, by creating API shaped with them in mind, so that they can grow in empowering directions.

Instead, we create monster trucks, and they put ads on youtube to tell everyone how "easy it is to drive" and "you should do it too right now".

For me, hooks are a monster truck. And most JS devs I work with are not capable of driving that safely.

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

#118

Earlier quoted context omitted.

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 have this doubt as well. Hooks will allow people to use "class like" features and behavior, but with none of the conventions forced by syntax, plus some pitfalls. To me it seems a good way to ensure most devs (espacially unexperienced casual ones, which are legions in JS by nature of the market) will dump their logic wherever it works with as little structure as it allows. It's a technical debt catalyser, and will…

I've also had doubts about hooks since its proposal, and I agree with the points you raised.

From the Rules of Hooks section in the documentation[0].

- React relies on the order in which Hooks are called. - Only call Hooks at the top level. Don’t call Hooks inside loops, conditions, or nested functions. - Only call Hooks from React function components. Don’t call Hooks from regular JavaScript functions.

This design decision doesn't sit well with me, and I won't be using it. However, the community seems to have already welcomed it, so I'm forced to learn the intricacies to be able to understand React codebases and keep up with the latest trend..

The addition of this feature is sure to increase cognitive load and technical debt. There are already countless variations of state management libraries and patterns that encourage pure functional components and decoupled logic. Hooks seem to be yet another opinionated strategy - and not for the best, in my view.

---

[0] https://reactjs.org/docs/hooks-rules.html

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

#119

Earlier quoted context omitted.

I have this doubt as well. Hooks will allow people to use "class like" features and behavior, but with none of the conventions forced by syntax, plus some pitfalls. To me it seems a good way to ensure most devs (espacially unexperienced casual ones, which are legions in JS by nature of the market) will dump their logic wherever it works with as little structure as it allows. It's a technical debt catalyser, and will…

I've also had doubts about hooks since its proposal, and I agree with the points you raised. From the Rules of Hooks section in the documentation[0]. - React relies on the order in which Hooks are called. - Only call Hooks at the top level. Don’t call Hooks inside loops, conditions, or nested functions. - Only call Hooks from React function components. Don’t call Hooks from regular JavaScript functions. This design d…

> However, the community seems to have already welcomed it, so I'm forced to learn the intricacies to be able to understand React codebases and keep up with the latest trend..

That's one thing that no many realizes: the JS community is special and you should take that in consideration when adding a new features.

When I say special, I mean it reacts (puns intended) in a way no other programming community does:

- it jumps on novelty with very little afterthoughs or regards for consequences.

- it has an habit of disregarding lessons from the past.

- it seldom fix problems, but add a new layer of solution on top of it.

Knowing this, the way hooks are released is too me like giving matches to children with the only advice "look how cool it is, I can make fireworks".

Yes, I'm exagerating for the argument sake. But still, react is now a software used everywhere, I'd welcome a switch from "move fast and break things" to "let's think this true" once in a while.

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

#120
post #64
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…

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 agree with you, but now with hooks that's not the case anymore.

It seems the React team intends to completely move away form using classes.

Post reply on HN