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

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

当前回答

这里我们不会在构造函数中获取这个,所以它将返回undefined,但是我们可以在构造函数外获取这个

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

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

如果我们使用super(),那么我们也可以在构造函数中获取“this”变量

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

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

当我们使用super();我们可以得到这个,但是这个。Props在构造函数中没有定义。除了构造函数,还有这个。Props不会返回undefined。

如果我们使用super(道具),那么我们可以使用这个。Props在构造函数中的值

苏菲·阿尔珀特的回答

如果你想用这个。构造函数中的道具,需要传递 为超级喝彩。否则,没关系,因为React会设置.props 调用后立即从外部调用实例 构造函数。

其他回答

这是我做的小提琴:jsfiddle.net。它表明默认情况下道具不是在构造函数中分配的。据我所知,它们是在方法React.createElement中分配的。因此,super(props)应该只在超类的构造函数手动将props赋给this.props时调用。如果你只是扩展React。组件调用super(props)不会对props做任何事情。也许它会在React的下一个版本中被改变。

这里我们不会在构造函数中获取这个,所以它将返回undefined,但是我们可以在构造函数外获取这个

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

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

如果我们使用super(),那么我们也可以在构造函数中获取“this”变量

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

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

当我们使用super();我们可以得到这个,但是这个。Props在构造函数中没有定义。除了构造函数,还有这个。Props不会返回undefined。

如果我们使用super(道具),那么我们可以使用这个。Props在构造函数中的值

苏菲·阿尔珀特的回答

如果你想用这个。构造函数中的道具,需要传递 为超级喝彩。否则,没关系,因为React会设置.props 调用后立即从外部调用实例 构造函数。

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

这个基类有一个自己的constructor()函数,里面有一些代码,用来为我们设置React组件。

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

为了确保React。组件的constructor()函数被调用时,我们调用super(props)。Super (props)是父构造函数()函数的引用,仅此而已。

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

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

定义这个constructor()函数的全部原因是初始化状态对象。

所以为了初始化我们的state对象,在super调用下面我将写:

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

      this.state = {};
   }

  // React says we have to define render()
  render() {
    return <div>Hello world</div>;
  }
};

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

只有一个原因需要将props传递给super():

当你想访问这个的时候。构造函数中的道具。

传递:

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', … }
    }
}

注意,传递或不传递道具给super对以后的使用没有影响。构造函数外的道具。也就是说,渲染、shouldComponentUpdate或事件处理程序总是可以访问它。

这一点在Sophie Alpert对一个类似问题的回答中得到了明确的表达。


文档-状态和生命周期,向类添加本地状态,第2点建议:

类组件应该总是使用props调用基构造函数。

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

(感谢@MattBrowne提供的链接)

按照源代码

function ReactComponent(props, context) {
  this.props = props;
  this.context = context;
}

你每次有道具的时候都要传递道具,不要把道具放到这里。手动道具。