question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Default lifecycle methods on React.Component ?

See original GitHub issue

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:

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:closed
  • Created 8 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
arcaniscommented, Oct 29, 2015

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!

0reactions
dylan-shaocommented, Jan 17, 2018

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found