【React和Vue的对比】

本文对比了React和Vue在state、props、refs、CSS、生命周期、路由、状态管理和子组件通信等方面的差异。React强调数据不变性,Vue采用双向绑定。在生命周期中,React有componentDidMount和shouldComponentUpdate,Vue有beforeCreate和updated。状态管理方面,React有Redux,Vue有Vuex。在组件通信上,React使用props和回调函数,Vue则使用$emit。
摘要由CSDN通过智能技术生成


三大属性

1.state

React

React强调数据的不变性,使用 this.setState()进行修改数据,它会以该组件为根,重新渲染整个组件子树。(如要避免不必要的子组件的重渲染,你需要在所有可能的地方使用 PureComponent,或是手动shouldComponentUpdate 方法。)

Vue

Vue是双向绑定,通过Object.defineProperty来监听data数据的改变(但是考虑性能问题,他只是做一层浅比较,无法对象或者数据的变化,可以用Vue.set进行添加,或者用push等Vue封装的方法)。

2.props

React和Vue都是单向数据流的

React
 <Hello list={
   this.state.list}>
//子组件通过this.props拿到
  this.props.list
Vue
//父组件
<Home  :msg='msg'/>
//子组件,会在这个组件实例的直下方
 props:["msg"]

3.refs

React
//三种方式创建
 1.<p ref='p'>123</p>
 2.<p ref={
   (c)=>{
   this.p = c}}>123</p>
 3.this.p = React.createRef();
//通过这个拿到
 this.refs
Vue
<input type="text" ref="input">
  this.$refs

css

React

行类样式

必须是{ {}}

<div style={
   {
   backgroundColor:'blue',color:'#ff671b'}}></div>
css模块化

import ChildrenCss from “./index.module.css”;
classnames库

css in js

通过js来操作css,社区的styled-component、jss等

 <div className='title'>
less
           {
   
              test: lessRegex,
              use: getStyleLoaders(
                {
   
                  importLoaders: 3,
                  sourceMap: isEnvProduction
                    ? shouldUseSourceMap
                    : isEnvDevelopment,
                },
                'less-loader'
              ),
              sideEffects: true,
            },

Vue

 title{
   
    font-size: 100px;
    background-color: rgb(18, 20, 20);
   }
 <div :style='title'>
 arrs:[title:true,other:false]
 <div :class='arrs'>
  
 <div :style={
   font-size: 100px, background-color: rgb(18, 20, 20)}>

生命周期

React

在这里插入图片描述

vue

在这里插入图片描述

Mounting 挂载顺序(初始化)

1.constructor(props):初始化 state 和 props 数据(相当于beforeCreate)
新增 static getDerivedStateFromProps(nextProps, prevState):组件初始化和被更新时调用用来替代
2.componentWillMount() 在组件即将被挂载到页面时执行(16.3已废弃)(相当于beforeMount)
3.render():渲染组件
4.componentDidMount():在组件被挂载到页面后执行,只在挂载时执行一次(相当于mounted)

Updation 更新顺序(更新)

1.static getDerivedStateFromProps(nextProps, prevState):组件初始化和被更新时调用
2.shouldComponentUpdate(nextProps, nextState):在组件被更新之前执行 (return true 更新 , return false 不更新)(相当于beforeUpdate)
3.render(): 渲染页面
4.static getSnapshotBeforeUpdate(prevProps, prevState)(相当于updated)
componentDidUpdate():state或props更新后调用

Unmounting 卸载(销毁)

1.componentWillUnmount():在组件即将被页面剔除时执行(相当于beforeDestroy)

vue生命周期

beforeCreate

created

beforeMount

mounted

更新(元素或组件的变更操作)

beforeUpdate

updated

销毁(销毁相关属性)

beforeDestroy

destroyed

路由

React路由

react-router-dom v6举例

//通过useRoutes进行路由的统一管理
const routes = [
    {
   
        path: '/about',
        element: <About />,
    },
    {
   
        path: '/home',
        element: <Home />,
    },
    {
   
        path:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值