junedomingo / movieapp

Discover Movies and TV shows - React Native
MIT License
1.81k stars 473 forks source link

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

Open grundmanise opened 7 years ago

grundmanise commented 7 years ago

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 commented 7 years ago

@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 commented 6 years ago

@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 commented 6 years ago

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 commented 6 years ago

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.