-
Notifications
You must be signed in to change notification settings - Fork 45.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Default lifecycle methods on React.Component ? #5047
Comments
We don't recommend inheritance for building your components. |
@arcanis inheritance gets pretty awkward when you have |
@spicyj Really ? Because that's what is documented here. (edit: Oh, sorry, I misread your comment: I should have read that plain classes are fine but inherited classes aren't, right? Well, it still sounds a bit strange to restrict the language features) @jedwards1211 Yep, I see what you mean. However, it can be solved by using static properties (already supported by Babel) and Object.assign to extend them from the parent to. |
Inheritance doesn't (and IMO shouldn't) work well for building React components.
What is your use case? |
Inherited classes are fine per se, it's just React won't go out of its way to encourage you to use inheritance. As you noted, you can use inheritance—it's just not very convenient, and for a good reason. For any custom logic you can use composition better, and the fact that Facebook has written a ton of React components and doesn't use inheritance is a great testament to that. |
Just wanted to make a little follow-up here : I've tried not using inheritance, and using composition instead and ... well, I really like it, actually. I've been able to achieve the same things than before and even more, but in a much cleaner way. When I need to override a behaviour, I just add a func prop to my "overridable" components and that's it. So I guess that as far as I'm concerned, this issue can be closed. I still think it would be nice to still add the default methods for those who prefer inheritance, but it's not something I would fight for. Thanks for the lesson! |
While it is true that we believe in composition over inheritance, I think the original question is still valid, so I'm tempted to leave this issue open.
The main reason I see for doing this: it allows static tooling to better reason about the code, rather than being forced to rely on implicit knowledge. For instance, I could mark a method as |
We won't be doing this. |
Hi @gaearon, I saw in the 'SplitPane ' example of https://facebook.github.io/react/docs/composition-vs-inheritance.html, |
Just a small API detail: would it make sense to implement default (empty) lifecycle methods in the base React.Component class? It would allow to have homogenous implementations as far as super() calls are concerned.
For example, the following will throw due to the missing
componentDidMount
implementation in the parent class:However, when overloading the
Foo
class, a missing super call might introduce some bugs. That's what got me to think that maybe it was safer to always put the super-call, and then I discovered that it was not possible when heriting from the base class.The text was updated successfully, but these errors were encountered: