Open grundmanise opened 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.
@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.
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".
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.
First of all thank you for the great bootstrap example!
Have few questions:
App
class a subclass of aComponent
i.e.class App extends Component
?new App();
to a constconst app = new App();
?