componentWillReceiveProps和componentDidUpdate區別


  參數 觸發時機 更新方式
componentWillReceiveProps

componentWillReceiveProps(nextProps)

只有一個參數nextProps,下一次的props

收到新的props之前做一些事情

僅在props變化時會觸發

更新狀態是同步的,   

不觸發重新render

componentDidUpdate

componentDidUpdate(preProps,preState,spanshot)

有三個參數,上一次的props,上一次的state,和快照

收到新的props或新的state之后做一些事情

props和state變化都會觸發,所有在此更新狀態一定要有判斷條件

更新狀態是異步的

觸發重新render

 

 

componentWillReceiveProps即將廢棄,推薦使用componentDidUpdate


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM