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?
What's the difference between state and props in React? In a React component, props are variables passed to it by its parent component. State on the other hand is still variables, but directly initialized and managed by the component.
“Props” is a special keyword in React, which stands for properties and is being used for passing data from one component to another.
For data that is going to change, we have to use state. And props are immutable while states are mutable, if you want to change props you can do from parent component and then pass it to child components.
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
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