Vue和React的区别

组件开发方式:
  1. Vue 使用单文件组件(SFC), HTML, JS 和 CSS 在一个文件内实现

    vue 复制代码
    <template>
      <div class="my-component">
        <!-- HTML模板 -->
      </div>
    </template>
    
    <script>
    export default {
      // JavaScript代码
    }
    </script>
    
    <style>
    .my-component {
      /* CSS样式 */
    }
    </style>
  2. React使用 JSX 和 JavaScript

    react 复制代码
    import React from 'react';
    import './MyComponent.css';
    
    function MyComponent() {
      return (
        <div className="my-component">
          {/* JSX 模板 */}
        </div>
      );
    }
    
    export default MyComponent;
数据响应:
  1. Vue3 基于响应式数据, 底层通过 new Proxy() 实现对数据变更的监控,相比于 React 更加的简单
  2. React 采用 setState or useState,手动的方式进行变更。
生命周期
  1. Vue3 生命周期更加灵活,8 个生命周期能够对组件各个阶段做到精确的控制

    beforeCreate、created、beforeMount、mounted、beforeUpdate、updated、beforeUnmount、unmounted

  2. React 生命周期更加精简

    constructor、componentDidMount、componentDidUpdate、componentWillUnmount

路由&状态管理
  1. Vue3 使用 Pinia/Vuex,router 使用 Vue-Router, 提供了一种简单和直接的状态管理方式
vue 复制代码
import { defineStore, acceptHMRUpdate } from 'pinia'
import { useUserStore } from './user'

export const useCartStore = defineStore({
  id: 'cart',
  state: () => ({
    rawItems: [] as string[],
  }),
  getters: {
    items: (state): Array<{ name: string; amount: number }> =>
      state.rawItems.reduce((items, item) => {
        const existingItem = items.find((it) => it.name === item)

        if (!existingItem) {
          items.push({ name: item, amount: 1 })
        } else {
          existingItem.amount++
        }

        return items
      }, [] as Array<{ name: string; amount: number }>),
  },
  actions: {
    addItem(name: string) {
      this.rawItems.push(name)
    },

    removeItem(name: string) {
      const i = this.rawItems.lastIndexOf(name)
      if (i > -1) this.rawItems.splice(i, 1)
    },

    async purchaseItems() {
      const user = useUserStore()
      if (!user.name) return

      console.log('Purchasing', this.items)
      const n = this.items.length
      this.rawItems = []

      return n
    },
  },
})

if (import.meta.hot) {
  import.meta.hot.accept(acceptHMRUpdate(useCartStore, import.meta.hot))
}
  1. React 使用 Redux/Mobx, router 使用 React-Router, 提供了更灵活的状态管理方案

    react 复制代码
    // actions
    export const Add = {
        type:'add'
    }
    export const Sub  = {
        type:'sub'
    }
    // reducer
    export default function counterReducer(state = initialState, action) {
        switch (action.type) {
            case 'add': return Object.assign({}, state, {
                count: state.count + 1
            });
            case 'sub': return Object.assign({}, state, {
                count: state.count - 1
            });
            default: return state;
        }
    }
    //设置一个初始值
    const initialState = {
        count: 0,
    }
视图功能
  1. Vue3 的视图通过 Vue3 定义的指令 + 模板的方式,包含样式,事件,表单,lot,DOM 节点操作, Provide/inject

    vue 复制代码
    <template>
      <div>
        <ul>
        	<li 
      			v-for="item,index in list" 
            @click="handleClick(index)"
            :style="{color: 'red'}"
              ></li>
        </ul>
        <div class="container">
          <header>
            <slot name="header"></slot>
          </header>
          <main>
            <slot></slot>
          </main>
          <footer>
            <slot name="footer"></slot>
          </footer>
    		</div>
          <div>
            <input name="username" :v-model="user.name" />
        	</div>
        </form>
      </div>
    </template>
    <script>
      methods: {
      	handleClick(index){
    
      }
    }
    </script>
  2. React 视图使用原生 JS + 模板的方式,包含样式,时间,表单,Children, DOM 节点操作, Context

react 复制代码
function MyComp() {
	return (
    <>
    	<ul ref="List">t
      { 
          list.map((v, i)=> <li onClick={handleClick(i)} style={{color: 'red'}}></li>)
      }
      </ul>
      <form>
        <div>
          <input name="username" onChange="(e) => handleInputChange(e)" value={user.name} />
      	</div>
    </form>
   </>
  )
}
    
const handleClick = (index) => {
    return () => {
        console.log(index)
    }    
}
相关推荐
遇到困难睡大觉哈哈2 小时前
Harmony os 静态卡片(ArkTS + FormLink)详细介绍
前端·microsoft·harmonyos·鸿蒙
用户47949283569153 小时前
Bun 卖身 Anthropic!尤雨溪神吐槽:OpenAI 你需要工具链吗?
前端·openai·bun
p***43483 小时前
前端在移动端中的网络请求优化
前端
g***B7383 小时前
前端在移动端中的Ionic
前端
大猩猩X4 小时前
vxe-gantt 甘特图使用右键菜单
vue.js·vxe-table·vxe-ui·vxe-gantt
拿破轮4 小时前
使用通义灵码解决复杂正则表达式替换字符串的问题.
java·服务器·前端
whltaoin4 小时前
【 Web认证 】Cookie、Session 与 JWT Token:Web 认证机制的原理、实现与对比
前端·web·jwt·cookie·session·认证机制
Aevget4 小时前
界面组件Kendo UI for React 2025 Q3亮点 - AI功能全面提升
人工智能·react.js·ui·界面控件·kendo ui·ui开发
Aerelin4 小时前
爬虫playwright入门讲解
前端·javascript·html·playwright
笙年4 小时前
JavaScript Promise,包括构造函数、对象方法和类方法
开发语言·javascript·ecmascript