gpt4 book ai didi

javascript - 如果我不将上下文参数传递给 React 中的 super 键会怎样?

转载 作者:行者123 更新时间:2023-11-29 23:27:34 26 4
gpt4 key购买 nike

因此,我看到许多开发人员/站点/代码/示例使用 super 关键字仅将 props 传递给父类,如下所示:

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

this.state = { someState: '' };
}

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

但实际上,传递给组件类的第二个参数是 context 并且许多开发人员/站点/代码/示例不会将它传递给父级,但从我的 Angular 来看它应该总是通过:

class Hello extends React.Component {
constructor(props, context) {
super(props, context);

this.state = { someState: '' };
}

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

所以我的问题是:

是否影响不传递上下文给父对象?

最佳答案

The context object will not be passed to your component unless you define contextTypes .

因此,在您的示例中,React 永远不会将 context 对象传递进来,因此将它向下传递给 super 构造函数是没有意义的。如果您愿意,您可以这样做,但这在功能上等同于不这样做。

关于javascript - 如果我不将上下文参数传递给 React 中的 super 键会怎样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48526100/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com