请问一下react constructor函数执行多少次?

react constructor函数执行多少次


慕的地6264312
浏览 1845回答 5
5回答

江户川乱折腾

class Update extends React.Component{constructor(props){super(props);this.state = {nowtime:'2016-12-12 17:00:00',resttime:'23'};}render(){return(<p id="updateTime">更新时间:{this.state.nowtime} 距离下一次更新大概还有{this.state.resttime}分钟</p>)}

天涯尽头无女友

原因调用super的原因:在ES6中,在子类的constructor中必须先调用super才能引用this。super(props)的目的:在constructor中可以使用this.props。最后,可以看下React文档,里面有一段:在es6中实现继承,直接调用super(name),就可以直接继承父类的属性和方法,所以super作用就相当于上述的实现继承的步骤,不过es6提供了super语法糖,简单化了继承的实现。
打开App,查看更多内容
随时随地看视频慕课网APP