從這個問題的答案是有道理的:If the props for a child component are unchanged, does React still re-render it?,我也做了一個CodePen來驗證它。爲什麼React如果其道具保持不變,會重投小孩?
shouldComponentUpdate(nextProps, nextState) {
console.log(nextProps.test === this.props.test);
console.log('shouldComponentUpdate');
return true;
}
但在陣營的網站一個說法是:
shouldComponentUpdate()
是正在接收新道具或狀態時,呈現之前調用。
但實際上,即使子組件的道具和狀態保持不變,shouldComponentUpdate()
仍然會被調用。官方聲明中是否存在一些錯誤,或者我誤解了它?
謝謝!我誤解爲'新的道具或狀態'。我曾經假設'新更新的道具或狀態'。 XD – Zhichao