-
Notifications
You must be signed in to change notification settings - Fork 113
Lifecycle methods: drop the 'component'. just 'willMount'. it's cleaner #40
Comments
@Gregoor is that idiomatic JS though? I don't think I've seen methods being grouped like this anywhere. Note that In the Personally I don't believe React should introduce its own variations on JavaScript class semantics, unless they're very widely applicable and already have a good chance of landing in a future ES spec. |
You're raising good points, thanks. The grouping is something that was born out of frustration about codebases I had to work with where the methods were all over the place. But I guess you're right and the benefit (clarity) doesn't outweigh the cost (having to rebind the methods) in this case. But I would still like to know if there is a chance that the methods will be renamed? |
Eh, I see you're reasoning; I certainly agree that it's a little verbose/superfluous. However, I would advocate against any such changes for two reasons:
All in all, @Gregoor, I am glad you asked this question. It brings up some interesting points for consideration. Whilst I think your proposal would be neither advantageous, nor very practical, I believe it does beg the question of how the community should design new additions to the react API as we go forward in the coming months and years. Assuming one is using |
@motiz88 I don't follow you when you say:
And in regard to you not seeing grouping like he used as a design pattern, I'd like to add: I've definitely seen it before but not really for Class/Object definitions, more like passing an object with your custom handlers/etc to a jQuery plugin. I don't care to search any examples, but it is certainly out there. And I agree, that's not where the community is moving. We're moving away from those semantics and towards something more OOP like you'd see in, um say, java. (thankgod) |
@rozzzly Invoking In userland, though, if it fits your project, you can always come up with your own abstractions/helpers and come very close to @Gregoor's suggested format. |
Thanks a lot for the detailed answer. Reading it was the opposite of a rigmarole (am I doing this right?). I do see that lifecycle methods not being called would be tricky to debug in a distant future, where all the deprecation messages would be gone. But then again, if someone only programs by copy pasting without even googling the used methods (which would bring up plenty of results informing about the change), that person would have it coming. 😛 Anyway, it's your baby and I will always love you for open-sourcing it, no matter what. Its probably a minor thing that makes more sense in my head because I don't have to deal with the negative consequences of it 😆 |
We've talked about this. Basically, it comes down to this: We value clarity and legibility over terseness. The "component" prefix makes things easier to grep for and makes it clearer that these are magic methods that React interprets specially. There's also a cost to a breaking change (as @rozzzly mentioned) which we'd rather avoid if we can. For these reasons, we don't plan to change this. |
thanks to @cpojer I can invalidate the "cost of API change"-argument. The grep and clarity arguments doesn't hold up in my opinion. Same would have to be true for render. Still, it's minor so feel free to ignore this. I was just looking for a reason to play around with codemods. |
Life-cycles are also considered "escape hatches", edge cases that you shouldn't normally need to use. They provide hooks for frameworks or edge cases rather than being encouraged in normal use. It's a classic framework trick to name discouraged methods with long names. :) Not sure how valid but seems to work. They also share the namespace with user code so they're less likely to collide if they're more explicit. It's probably safe to apply a codemod but even a codemod requires work/testing so we try to keep those to a minimum when it buys us something substantial. |
Fair enough, with that in mind I just looked through my code bases and found that most of the time when I use lifecycles, it's to interop with a non-React lib. Thanks! :) |
Probably OT but i always felt the same way as @Gregoor |
@sebmarkbage Maybe Symbols is better than long names for such usage 😃 |
I would like to add that if an object from which you construct a component is a rich object (it can be literally anything with a |
Well if you would be supplying a "rich object" to React, I would imagine that you're violating single responsibility and should rather go with composition in that case. |
@Gregoor , I agree. Personally I tend to apply composition whenever possible. However, I don't see a reason to reduce available options which the current API allows. |
Pardon the stupid joke.
I was curious what was the rationale (if there is one) behind having 'component' in the lifecycle methods. To me it seems unnecessary as they are clearly in the context of a component. What would make sense to me though is grouping the lifecycle methods like this:
The text was updated successfully, but these errors were encountered: