Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

reactjs

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?

like image 225
reectrix Avatar asked Aug 30 '16 14:08

reectrix


People also ask

What is the difference between * props * and * State * In React?

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.

What does this props mean in React?

“Props” is a special keyword in React, which stands for properties and is being used for passing data from one component to another.

What is the difference between state and props why you can't update props in React?

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.


1 Answers

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
like image 96
Rafi Ud Daula Refat Avatar answered Sep 30 '22 18:09

Rafi Ud Daula Refat