Vue和React生命周期的对比,代码演示

Vue和React是两个非常流行的前端框架,它们在生命周期管理方面有一些相似和不同之处。

Vue的生命周期包括:beforeCreate、created、beforeMount、mounted、beforeUpdate、updated、beforeDestroy和destroyed。

React的生命周期包括:constructor、getDerivedStateFromProps、render、componentDidMount、shouldComponentUpdate、getSnapshotBeforeUpdate、componentDidUpdate、componentWillUnmount。

下面是一个使用Vue和React分别展示生命周期的示例代码:

Vue代码示例:

<template>
  <div>{{ message }}</div>
</template>

<script>
export default {
  data() {
    return {
      message: 'Hello Vue!'
    };
  },
  beforeCreate() {
    console.log('beforeCreate');
  },
  created(){
    console.log('created');
  },
  beforeMount() {
    console.log('beforeMount');
  },
  mounted() {
    console.log('mounted');
  },
  beforeUpdate() {
    console.log('beforeUpdate');
  },
  updated() {
    console.log('updated');
  },
  beforeDestroy() {
    console.log('beforeDestroy');
  },
  destroyed() {
    console.log('destroyed');
  }
};
</script>

React代码示例:

import React, { Component } from 'react';

class MyComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      message: 'Hello React!'
    };
    console.log('constructor');
  }

  static getDerivedStateFromProps(props, state) {
    console.log('getDerivedStateFromProps');
    return state;
  }

  componentDidMount() {
    console.log('componentDidMount');
  }

  shouldComponentUpdate(nextProps, nextState) {
    console.log('shouldComponentUpdate');
    return true;
  }

  getSnapshotBeforeUpdate(prevProps, prevState) {
    console.log('getSnapshotBeforeUpdate');
    return null;
  }

  componentDidUpdate(prevProps, prevState, snapshot) {
    console.log('componentDidUpdate');
  }

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

  render() {
    console.log('render');
    return <div>{this.state.message}</div>;
  }
}

这些生命周期方法在组件的不同阶段会被调用,可以通过在控制台输出日志来观察它们的调用顺序。

需要注意的是,React 16.3版本之后引入了新的生命周期API,并且有一些旧的生命周期方法已被标记为过时。在新的React项目中,建议使用函数组件和React Hooks来管理组件的状态和生命周期。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
VueReact是两个流行的前端框架,它们的生命周期在概念上有些不同。下面是VueReact生命周期的对比: Vue生命周期: 1. beforeCreate:在实例初始化之后,数据观测和事件配置之前调用。 2. created:在实例创建完成后调用,在这一步,实例已完成数据观测,属性和方法的运算,但是还没有进行DOM的挂载。 3. beforeMount:在挂载开始之前被调用,相关的render函数首次被调用。 4. mounted:挂载完成后调用,此时,组件已经显示到页面上。 5. beforeUpdate:在数据更新之前调用,在这一步,可以对更新之前的状态进行修改。 6. updated:数据已经更新完成后调用,此时,组件已经重新渲染完成。 7. beforeDestroy:在实例销毁之前调用,此时,实例仍然完全可用。 8. destroyed:实例销毁之后调用,此时,组件已经被完全卸载,所有的事件监听器和子实例已经被移除。 React生命周期: 1. constructor:在组件被创建时调用,可以进行初始化工作。 2. static getDerivedStateFromProps:在组件接收到新的props时调用,返回新的state值。 3. render:根据props和state渲染组件的UI。 4. componentDidMount:组件挂载后调用,可以进行异步数据获取等操作。 5. shouldComponentUpdate:在组件更新前调用,用于决定是否需要重新渲染组件。 6. getSnapshotBeforeUpdate:在组件更新前调用,用于获取更新前的DOM状态。 7. componentDidUpdate:组件更新后调用,可以进行DOM操作。 8. componentWillUnmount:组件即将卸载时调用,进行清理操作。 需要注意的是,React 16.3版本之后引入了新的生命周期方法,如getDerivedStateFromProps和getSnapshotBeforeUpdate,用于替代之前的生命周期方法。而Vue生命周期相对较简单,并且更注重于组件的渲染和数据变化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

No DeBug

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值