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