react生命周期的執(zhí)行順序是什么

小億
111
2024-02-22 17:26:30

React生命周期的執(zhí)行順序如下:

  1. 初始化階段(Mounting):組件被創(chuàng)建并插入到DOM中

    • constructor()
    • static getDerivedStateFromProps()
    • render()
    • componentDidMount()
  2. 更新階段(Updating):組件的props或state發(fā)生變化,導(dǎo)致重新渲染

    • static getDerivedStateFromProps()
    • shouldComponentUpdate()
    • render()
    • getSnapshotBeforeUpdate()
    • componentDidUpdate()
  3. 卸載階段(Unmounting):組件被從DOM中移除

    • componentWillUnmount()
  4. 錯(cuò)誤處理階段(Error Handling):組件渲染過程中出現(xiàn)錯(cuò)誤

    • static getDerivedStateFromError()
    • componentDidCatch()

0