React 運行流程圖,如下
componentWillReceiveProps在初始化render的時候不會執行,它會在Component接受到新的狀態(Props)時被觸發,一般用於父組件狀態更新時子組件的重新渲染。
// 這種方式十分適合父子組件的互動,通常是父組件需要通過某些狀態控制子組件渲染亦或銷燬
componentWillReceiveProps(nextProps) {
//componentWillReceiveProps方法中第一個參數代表即將傳入的新的Props
if (this.props.sharecard_show !== nextProps.sharecard_show){
//在這裏我們仍可以通過this.props來獲取舊的外部狀態
//通過新舊狀態的對比,來決定是否進行其他方法
if (nextProps.sharecard_show){
this.handleGetCard();
}
}
}