1

While experimenting with React Native and using States. I came against super(props). I searched here in the forum for a useful explaination, but I didn't find any.

Here is a look on the function, where I am using super and constructor... 1

I tried to delete props parameter in constructor, but it gave me an error. Then I tried to delete super(props), it gave me again an error. However, the code perfectly work, when I just used super() without parameter.

My conclusion is, in constructor i am saying we are using props and with super I am allowing an access over all props globally in code?

I am not really sure, correct me please If I am wrong.

I appreciate any comments with advises. Thanks in advance!

Edit:

I also added the rest of the code...For a clear undestanding.. 2

I appreciate your help..

NekoMisaki
  • 101
  • 1
  • 6
  • 1
    Possible duplicate of [Inheritance and Super in JavaScript](https://stackoverflow.com/questions/41180655/inheritance-and-super-in-javascript) – Jared Smith Mar 15 '18 at 14:26
  • Did you try googling e.g. "super javascript"? – Jared Smith Mar 15 '18 at 14:26
  • @JaredSmith yes I did, but somehow I think my case here is different. It would be helpful if you could explain :) – NekoMisaki Mar 15 '18 at 14:49
  • `super` by itself calls the parent class constructor on the current instance with the supplied parameters and must be called in every subclass prior to the first use of `this`. – Jared Smith Mar 15 '18 at 14:59
  • @JaredSmith but if I don't want to use this, I don't need to load props in super, right? What also weird is that I can use 'this' in render in my subclass even if super isn't loaded with props. How is this possible? – NekoMisaki Mar 15 '18 at 15:10

1 Answers1

1

In React Native when using super(props) you can immediately access the props via this.props in the constructor while with only super() you can't.

In other methods like render however you can always utilize this.props.

Here is a complete examples of the explaination above:

https://stackoverflow.com/a/34995257/4293498

Chris
  • 1,008
  • 2
  • 11
  • 22