Reactjs 什么';这两者之间的区别是什么;超级();及;超级(道具)“;在使用es6类时如何反应?

Reactjs 什么';这两者之间的区别是什么;超级();及;超级(道具)“;在使用es6类时如何反应?,reactjs,ecmascript-6,Reactjs,Ecmascript 6,什么时候将道具传递给super()很重要,为什么 class MyComponent extends React.Component { constructor(props) { super(); // or super(props) ? } } 依照 每次你有道具时,你都必须通过props,而不是手动将它们放入this.props。这是我做的小提琴:。它表明默认情况下,道具不在构造函数中分配。据我所知,它们在方法React.createElement中分配。因此,只有当超类的

什么时候将
道具
传递给
super()
很重要,为什么

class MyComponent extends React.Component {
  constructor(props) {
    super(); // or super(props) ?
  }
}
依照


每次你有道具时,你都必须通过
props
,而不是手动将它们放入
this.props

这是我做的小提琴:。它表明默认情况下,道具不在构造函数中分配。据我所知,它们在方法
React.createElement
中分配。因此,只有当超类的构造函数手动将
props
分配给
this.props
时,才应该调用
super(props)
。如果您只是扩展
React.Component
调用
super(props)
将无法使用props。它可能会在React的下一个版本中更改。

super()
用于调用父构造函数

super(props)
props
传递给父构造函数

在您的示例中,
super(props)
将调用
React.Component
构造函数作为参数传入
props

有关
super
的更多信息:

在本例中,您正在扩展
React.Component
类,并且根据ES2015规范,子类构造函数在调用
super()
之前不能使用
;此外,如果ES2015类构造函数是子类,那么它们必须调用
super()

class MyComponent extends React.Component {
  constructor() {
    console.log(this); // Reference Error
  }

  render() {
    return <div>Hello {this.props.name}</div>;
  }
}

我从一些与我交谈过的开发人员身上看到的一个困惑点是,没有
构造函数的组件,因此没有在任何地方调用
super()
,仍然在
render()
方法中提供
this.props
。请记住,此规则以及为
构造函数创建
绑定的这一需要仅适用于
构造函数
当需要将
道具
传递给
super()
时,只有一个原因:

当您想要访问构造函数中的
此.props
时。

通过:

class MyComponent extends React.Component {    
    constructor(props) {
        super(props)

        console.log(this.props)
        // -> { icon: 'home', … }
    }
}
未通过:

class MyComponent extends React.Component {    
    constructor(props) {
        super()

        console.log(this.props)
        // -> undefined

        // Props parameter is still available
        console.log(props)
        // -> { icon: 'home', … }
    }

    render() {
        // No difference outside constructor
        console.log(this.props)
        // -> { icon: 'home', … }
    }
}
请注意,传递或不传递
props
super
对以后使用
这个.props
构造函数之外的
没有影响。也就是说,
呈现
应该组件更新
,或者事件处理程序总是可以访问它

索菲·阿尔伯特在一篇关于类似问题的文章中明确地提到了这一点


文件建议:

类组件应始终使用
props
调用基构造函数

但是,没有提供任何理由。我们可以推测这要么是因为子类化,要么是为了将来的兼容性


(感谢@MattBrowne的链接)

当您将
道具
传递给
super
时,道具将被分配给
。请看以下场景:

constructor(props) {
    super();
    console.log(this.props) //undefined
}
当你这样做的时候:

constructor(props) {
    super(props);
    console.log(this.props) //props will get logged.
}

在React组件中实现
constructor()
函数时,需要
super()
。请记住,您的
MyComponent
组件正在扩展或借用
React.component
基类的功能

这个基类有一个自己的
constructor()
函数,其中包含一些代码,用于为我们设置React组件

当我们在
MyComponent
类中定义
constructor()
函数时,我们实际上是在重写或替换
React.Component
类中的
constructor()
函数,但我们仍然需要确保该
constructor()中的所有设置代码
函数仍会被调用

因此,为了确保调用
React.Component
constructor()
函数,我们调用
super(props)
super(props)
是对父类
constructor()
函数的引用,仅此而已

每次在基于类的组件中定义
constructor()
函数时,我们都必须添加
super(props)

如果我们不这样做,我们将看到一个错误,说我们必须调用
super(props)

定义此
构造函数()
函数的全部原因是初始化状态对象

为了初始化我们的状态对象,在超级调用下面我将写:

class App extends React.Component {
  constructor(props) {
      super(props);

      this.state = {};
   }

  // React says we have to define render()
  render() {
    return <div>Hello world</div>;
  }
};
类应用程序扩展了React.Component{
建造师(道具){
超级(道具);
this.state={};
}
//React说我们必须定义render()
render(){
返回你好世界;
}
};

因此,我们定义了
constructor()
方法,通过创建一个JavaScript对象初始化状态对象,为其分配一个属性或键/值对,并将结果分配给
this.state
。当然,这只是一个例子,所以我没有给state对象分配一个键/值对,它只是一个空对象。

这里我们不会在构造函数中得到它,所以它将返回未定义的,但是我们可以在构造函数函数之外获取它

class MyComponent extends React.Component {
  constructor() {
    console.log(this); // Reference Error i.e return undefined
  }

  render() {
    return <div>Hello {this.props.name}</div>;
  }
}
类MyComponent扩展了React.Component{ 构造函数(){ console.log(this);//引用错误,即返回未定义 } render(){ 返回Hello{this.props.name}; } }
如果我们使用super(),那么我们也可以在构造函数中获取“this”变量

class MyComponent extends React.Component {
  constructor() {
    super();
    console.log(this); // this logged to console
  }

  render() {
    return <div>Hello {this.props.name}</div>;
  }
}
类MyComponent扩展了React.Component{ 构造函数(){ 超级(); console.log(this);//此日志记录到控制台 } render(){ 返回Hello{this.props.name}; } }
所以当我们使用super()时;我们将能够获取这个,但this.props将在构造函数中未定义。但除构造函数外,this.props不会返回未定义。

如果我们使用超级道具
class App extends React.Component {
  constructor(props) {
      super(props);

      this.state = {};
   }

  // React says we have to define render()
  render() {
    return <div>Hello world</div>;
  }
};
class MyComponent extends React.Component {
  constructor() {
    console.log(this); // Reference Error i.e return undefined
  }

  render() {
    return <div>Hello {this.props.name}</div>;
  }
}
class MyComponent extends React.Component {
  constructor() {
    super();
    console.log(this); // this logged to console
  }

  render() {
    return <div>Hello {this.props.name}</div>;
  }
}