Skip to content
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

What's the purpose of making an App class a subclass of a Component #17

Open
grundmanise opened this issue Jun 14, 2017 · 4 comments
Open

Comments

@grundmanise
Copy link

First of all thank you for the great bootstrap example!

Have few questions:

  1. What's the purpose of making an App class a subclass of a Component i.e. class App extends Component?
  2. And also why do you assign new App(); to a const const app = new App();?
@grundmanise
Copy link
Author

@rvpyrv I know OOP, and I know what Component does. I'll rephrase my question:
IMO making App a class is an overkill, why not just make it an object App = { init(){ /* init code */} }; without making it a class, because it does not utilise any of Components methods or render a component.
Then in index.whatever.js you can bootstrap with App.init(). In my app I bootstrap like this without any issues.

@evilDave
Copy link

@grundmanise Agreed. I don't see why you would need to make App a component. The old example does not do it - indeed it has a comment that it is not a component.

@joergbaier
Copy link

It's to have access to the React lifecycle methods, in this case constructor. While not needed it was most likely done to make the app more "React like".

@vovkasm
Copy link

vovkasm commented Nov 18, 2017

Also constructor is React lifecycle method, it is also "generic" lifecycle method of any class in JS (if this class can produce objects of course).
Making App subclass of React.Component simple mistake, because App is not conform to interface of React.Component. Next sentence from https://reactjs.org/docs/react-component.html

React.Component is an abstract base class, so it rarely makes sense to refer to React.Component directly. Instead, you will typically subclass it, and define at least a render() method.

So to be React.Component, class should implement render() method, without which it is not component, because it can't produce react elements by standard way. App is not React.Component also because it can't be passed to any function that expect React.Component.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants