學習React(19) - 組件生命週期方法 (Component lifecycle methods)

React給我們提供了建立的方法,我們就可以用這些方法來override 在一些特殊的stages。這些方法不能在函數模塊中使用。將這些方法分爲四種類別。

  1. Mounting: When an instance of a component is being created and inserted into the DOM.
    這裏一共有四種方法:constructor, static getDerivedStateFromProps, render, and componentDidMount.

  2. Updating: When a component is being re-rendered as a result of changes to either its props or state.
    這裏一共有五種方法:static getDerivedStateFromProps, shouldComponentUpdate, render, getSnapshotBeforeUpdate, componentDidUpdate

  3. Unmounting: When a component is being removed from the DOM.
    這裏只有一種方法:componentWillUnmount

  4. Error Handling: When there is an error during rendering, in a lifecycle method, or in the constructor of any child component.
    這裏只有兩種方法:static getDerivedStateFromError, componentDidCatch

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章