![](/img/trans.png)
[英]Why is child component re-rendering if only prop has useCallback?
[英]Passing prop down to the child is causing Re-rendering of all the owners
我有一個組件PP
,它有一個裝飾器, patternStreams
每隔幾秒鍾接收一個道具patternStreams
。 PP
擁有一個名為PPView
的組件,並將其傳遞給patternStreams
, PPView
擁有一個RP
組件,並將其傳遞給patternStreams
。
通過將console.log
放置在每個組件的render()
方法中,我注意到當將prop傳遞給PP
組件時,所有這三個組件每隔幾秒鍾就會重新繪制一次。
唯一需要重繪的組件是RP
,那么如何防止其余組件被重繪呢?
有一個叫生命周期方法shouldComponentUpdate
已經做了你需要的東西- https://facebook.github.io/react/docs/component-specs.html#updating-shouldcomponentupdate
接收新的道具或狀態時在渲染之前調用。 初始渲染或使用forceUpdate時不調用此方法。
當您確定過渡到新的道具和狀態不需要更新組件時,可以利用此機會返回false。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.