gpt4 book ai didi

reactjs - ES6 类中对 super(props) 的调用重要吗?

转载 作者:行者123 更新时间:2023-12-03 13:19:45 24 4
gpt4 key购买 nike

假设我有以下类(class):

class Tabs extends React.Component {
displayName: Tabs;

static propTypes = {
selected: React.PropTypes.number,
children: React.PropTypes.oneOfType([
React.PropTypes.array,
React.PropTypes.element
]).isRequired
};

constructor() {
super();
this.state = {
selected: 0,
maxSelected: 0
};

render() {
return(
<div>
{this.props.selected}
{this.props.children}
</div>
);
}
};

我想知道传递以下构造函数是否重要:

constructor(props) {
super(props);
}

我当前的代码工作得很好,但我想知道这是否是一个好的做法。

最佳答案

根据Sophie Alpert with the React team如果您打算在构造函数内使用 this.props ,则只需将 props 传递到构造函数中。调用构造函数后,React 会从外部将 props 附加到组件。

关于reactjs - ES6 类中对 super(props) 的调用重要吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35835371/

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