am5255 am5255 - 3 months ago 13
React JSX Question

Is there a difference between using this.props vs props in React?

After you've defined

this
, is there a difference between using
this.props
and
props
?

constructor(props) {
super(props);

this.state = {
input: this.props.input,
input2: props.input2
};
}


In this case, what the difference between
input
and
input2
here?

Answer

The rules for ES2015 (ES6) classes basically come down to:

In a child class constructor, this cannot be used until super is called.

ES6 class constructors MUST call super if they are subclasses, or they must explicitly return some object to take the place of the one that was not initialized.

So as the super(props) is called in the constructor. props and this.props are same. so,

props === this.props // true
Comments