我一直在将我的一个应用程序迁移到 Node/React 上的 ES6,我有一个关于如何将 props 传递给 child 的问题。我读了很多帖子,有些讨论了这个问题,有些则没有。基本上,到目前为止我所看到的是:
export default class SomeComponent extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
{this.props.text} <<< Props used here
</div>
);
}
}
但我已经能够让我的组件与以下内容一起工作:
export default class SomeComponent extends React.Component {
constructor() {
super(); <<< notice no props in parentheses
}
render() {
return (
<div>
{this.props.text} <<< Props used here
</div>
);
}
}
为什么我应该在构造函数和 super 调用的括号中传递 props ?或者我可以保留我的代码吗
最佳答案
除非你想在构造函数中使用 this.props,否则你不需要将 props 传递给 super。
关于reactjs - es6 React在子组件中获取 Prop ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36869451/