Default lifecycle methods on React.Component ?
See original GitHub issueJust 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:
class Foo extends React.Component {
componentDidMount( ) {
super.componentDidMount( );
}
};
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.
Issue Analytics
- State:
- Created 8 years ago
- Comments:9 (7 by maintainers)
Top Results From Across the Web
React.Component
Each component has several “lifecycle methods” that you can override to run code at particular times in the process. You can use this...
Read more >React lifecycle methods: An approachable tutorial with examples
In React, components go through a lifecycle of events: ... You can think of these events as a component's birth, growth, and death,...
Read more >ReactJS | Lifecycle of Components - GeeksforGeeks
React provides the developers a set of predefined functions that if present is invoked around specific events in the lifetime of the component....
Read more >Learn React: Lifecycle Methods Cheatsheet | Codecademy
React supports three mounting lifecycle methods for component classes: componentWillMount() , render() , and componentDidMount() . componentWillMount() will be ...
Read more >What Are the React Component Lifecycle Methods?
Lifecycle Methods : · Static getDerivedstatefromprops · shouldComponentUpdate · Render · getSnapshotBeforeUpdate · componentDidUpdate.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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!
Hi @gaearon, I saw in the 'SplitPane ’ example of https://facebook.github.io/react/docs/composition-vs-inheritance.html, the stateless component ‘SplitPane’ wraps other component like the ‘Chat’. But usually the ‘Chat’ component is stateful, so it ends with a stateless component wrap a stateful component, is it a good practice?(I mean, I have been told stateless component should not have stateful child ) Thanks!