ElJ*_*mes 0 meteor ecmascript-6 reactjs
根据标题,为什么最初的this.props失败?实际上,如果您依赖构造函数中的props,该如何解决呢?例如,我想在订阅中引用道具吗?
class AboutBox extends Component {
static defaultProps = {
title: 'Undefined Product',
}
constructor() {
super();
console.log(this.props.title); //this fails (=> null)
}
render() {
console.log(this.props.title); //this works (=> 'Undefined Product')
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
您必须将道具放在构造函数args上,并将它们传递给super
constructor(props){
super(props);
console.log(props.title);
}
Run Code Online (Sandbox Code Playgroud)
在构造函数中使用props,这也可以,但是在针对IE的转译过程中存在一些问题
| 归档时间: |
|
| 查看次数: |
2343 次 |
| 最近记录: |