React給我們提供了建立的方法,我們就可以用這些方法來override 在一些特殊的stages。這些方法不能在函數模塊中使用。將這些方法分爲四種類別。
-
Mounting: When an instance of a component is being created and inserted into the DOM.
這裏一共有四種方法:constructor, static getDerivedStateFromProps, render, and componentDidMount. -
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 -
Unmounting: When a component is being removed from the DOM.
這裏只有一種方法:componentWillUnmount -
Error Handling: When there is an error during rendering, in a lifecycle method, or in the constructor of any child component.
這裏只有兩種方法:static getDerivedStateFromError, componentDidCatch