在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ react class 的 constructor方法為什么要帶props

react class 的 constructor方法為什么要帶props

class Clock extends React.Component {
  constructor(props) {
    super(props);
    this.state = {date: new Date()};
  }

這里的props是什么意思,是內(nèi)置的對象嗎?看到這種寫法很疑惑。

回答
編輯回答
故林

props就是傳遞給你組件的props。在constructor中,如果不需要取props中的值,是可以簡寫的。

constructor() {
  super();
}

寫出來,一般是想給組件設(shè)置初始化的state。如:

constructor(props) {
  super();
  this.state = {
    xxx: props.xxx
  }
}

// 然后再配合componentWillReceivePorps來實現(xiàn)props和state同步的目的
componentWillReceiveProps(np) {
}
2017年6月13日 20:17