I am not great with JS and playing around with React.
The React docs located here state the following:
When implementing the constructor for a
React.Componentsubclass, you should callsuper(props)before any other statement. Otherwise,this.propswill be undefined in the constructor, which can lead to bugs.
My question is HOW does this actually work? What is super() doing that magically enables this.props within my constructor?
In the documentation that you have mentioned. It is coded in ES6 standard of javascript.
So this statement
class Greeting extends React.Component
It means Greeting is inherting from React.Component, by calling super, you are actually calling the parent element with props parameter,
if you intend on using this.props inside the constructor, you have to call super(props)
Hope these links are helpful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With