使用useEffec来分别模拟class的生命周期

本文借鉴于此

一、useEffect

useEffect 拥有两个参数,第一个参数作为回调函数会在浏览器布局和绘制完成后调用,因此它不会阻碍浏览器的渲染进程。

第二个参数是一个数组:

当数组存在并有值时,如果数组中的任何值发生更改,则每次渲染后都会触发回调。
当它不存在时,每次渲染后都会触发回调。
当它是一个空列表时,回调只会被触发一次,类似于 componentDidMount。

二、componentDidMount(组件挂载完的时刻)

1、class 组件访问 componentDidMount

class Example extends React.Component {
  componentDidMount() {
    console.log('I am mounted!');
  }
  render() {
    return null;
  }
}

2、使用 hooks 模拟 componentDidMount

function Example() {
  useEffect(() => console.log('mounted'), []); //第二个参数为空数组,只会触发回调一次
  return null;
}

三、shouldComponentUpdate(组件更新之前)

1、class 组件访问 shouldComponentUpdate

shouldComponentUpdate(nextProps, nextState){
  console.log('shouldComponentUpdate')
  // return true 更新组件
  // return false 则不更新组件
}

2、hooks 模拟 shouldComponentUpdate

const MyComponent = React.memo(
    _MyComponent, 
    (prevProps, nextProps) => nextProps.count !== prevProps.count
)

React.memo 包裹一个组件来对它的 props 进行浅比较,但这不是一个 hooks,因为它的写法和 hooks 不同,其实React.memo 等效于 PureComponent,但它只比较 props。

四、componentDidUpdate(在组件更新之后执行)

1、class 组件访问 componentDidUpdate

componentDidMount() {
  console.log('mounted or updated');
}

componentDidUpdate() {
  console.log('mounted or updated');
}

2、使用 hooks 模拟 componentDidUpdate

useEffect(() => console.log('mounted or updated'));

值得注意的是,这里的回调函数会在每次渲染后调用,因此不仅可以访问 componentDidUpdate,还可以访问componentDidMount,如果只想模拟 componentDidUpdate,我们可以这样来实现:

const mounted = useRef();
useEffect(() => {
  if (!mounted.current) {
    mounted.current = true;
  } else {
   console.log('I am didUpdate')
  }
});

useRef 在组件中创建“实例变量”。它作为一个标志来指示组件是否处于挂载或更新阶段。当组件更新完成后在会执行 else 里面的内容,以此来单独模拟 componentDidUpdate。

五、componentWillUnmount(组件将要挂载到页面的时刻)

1、class 组件访问 componentWillUnmount

componentWillUnmount() {
  console.log('will unmount');
}

2、hooks 模拟 componentWillUnmount

useEffect(() => {
  return () => {
    console.log('will unmount');
  }
}, []);

当在 useEffect 的回调函数中返回一个函数时,这个函数会在组件卸载前被调用。我们可以在这里面清除定时器或事件监听器。

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React Class组件的生命周期在不同的React版本中有所变化。在旧版本中,常用的生命周期方法包括componentWillMount、componentDidMount、componentWillReceiveProps、componentDidUpdate和componentWillUnmount等。而在较新的版本(v16.3之后),一些生命周期方法被废弃,同时也新增了一些生命周期方法。 旧版本中,componentWillMount在组件即将被渲染前调用,componentDidMount在组件被挂载到DOM后调用,componentWillReceiveProps在组件接收到新的props时调用,componentDidUpdate在组件更新后调用,componentWillUnmount在组件即将被卸载前调用。 然而,由于这些生命周期方法存在一些副作用,并且在新的异步渲染模式下可能会引起一些问题,所以在新版本中,一些生命周期方法被废弃了。相反,一些新的生命周期方法被引入,如static getDerivedStateFromProps和componentDidUpdate。getDerivedStateFromProps用于替代componentWillReceiveProps,它接收到新的props并返回一个新的state值。componentDidUpdate则在组件更新后被调用。 总而言之,在React Class组件中,生命周期方法的使用和调用顺序取决于React的版本,需要根据具体的版本文档来进行参考和使用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [ReactClass类组件的生命周期](https://blog.csdn.net/qq_31281245/article/details/127288932)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [React Class组件生命周期详解](https://blog.csdn.net/qq_43293207/article/details/117171627)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值