代码如下,在父类的componentWillMount方法中输出 this,打印的却是子类 item
通过webpack的编译后文件中可以看到,Item方法中实际上有注入一个 _List参数,
问题: react 中的extends 与 es6中的extends的有什么区别吗?
class List extends React.Component{
constructor(props){
super(props);
this.state = {
List: [4,5]
}
}
componentWillMount(){
console.log(this);
console.log(this.state.List);
}
}
class Item extends List{
constructor(props){
super(props);
this.state = {
List : [1,2,3]
}
}
render(){
return (
<div>123</div>
)
}
}
React.render(<Item />, document.getElementById('app'));
没有区别;
组件不要用继承。