javascript - 为什么react的组件要super(props)
本文介绍了javascript - 为什么react的组件要super(props)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
如图,我知道supert是继承constructor的参数,但是为什么在react里面,有一些组件使用了super(props),而有一些没有写,还有在es6里就只是写了supert(),这些区别在哪呢?以及这里的这个constructor(props)...super(props)是起到什么作用呢
这个是全代码:
解决方案
调用super的原因:在ES6中,在子类的
constructor
中必须先调用super
才能引用this
super(props)
的目的:在constructor
中可以使用this.props
最后,可以看下React文档,里面有一段
Class components should always call the base constructor with props.
这篇关于javascript - 为什么react的组件要super(props)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文