React—constructor和super

如果你用到了constructor就必须写super(),是用来初始化this的,可以绑定事件到this;

如果你在constructor中要使用this.props,就必须给super加参数:super(props)

(无论有没有constructor,在renderthis.props都是可以使用的,这是React自动附带的;)

如果没用到constructor,是可以不写的React会默认添加一个空的constructor。

猜你喜欢

转载自www.cnblogs.com/dreamhouse/p/9084254.html