如何通过React挂钩在功能组件中使用componentWillUpdate?

时间:2019-11-16 13:06:14

标签: reactjs react-hooks react-lifecycle-hooks

如何通过react钩子在功能组件中使用componentWillUpdate?

1 个答案:

答案 0 :(得分:1)

您可以结合使用ref和效果钩子来创建与 componentWillUpdate 相同的效果。

Docs says componentWillUpdate ,每次发生更新时都会调用。在初始渲染时不会调用。

现在使用 useRef 钩子,可以在应用程序的整个生命周期内保留被粉碎对象。

const isFirstRender = React.useRef(true);
  React.useEffect(() => {
    if (isFirstRender.current) {
      isFirstRender.current = false;
      return;
    }
  /*business logic for component did update*/
      
  });