当前位置: 首页 > news >正文

shallowRef和shallowReactive的用法以及使用场景和ref和reactive的区别

Vue3 浅层响应式 API

1. ref vs shallowRef

1.1 基本概念

  • ref: 深层响应式,会递归地将对象的所有属性转换为响应式
  • shallowRef: 浅层响应式,只有 .value 的改变会触发更新,不会递归转换对象的属性

1.2 使用对比

// ref 示例
const deepRef = ref({count: 0,nested: {value: 'hello'}
})// 这些修改都会触发更新
deepRef.value.count++  // 触发更新
deepRef.value.nested.value = 'world'  // 触发更新// shallowRef 示例
const shallowValue = shallowRef({count: 0,nested: {value: 'hello'}
})// 只有直接替换 .value 才会触发更新
shallowValue.value.count++  // 不会触发更新
shallowValue.value.nested.value = 'world'  // 不会触发更新
shallowValue.value = { count: 1 }  // 触发更新

1.3 性能优化示例

<template><div><video ref="videoRef" :src="videoUrl"></video><button @click="updateVideoUrl">更新视频</button></div>
</template><script setup>
import { shallowRef } from 'vue'// 使用 shallowRef 优化大型数据或 DOM 引用
const videoRef = shallowRef(null)
const videoUrl = shallowRef('https://example.com/video.mp4')const updateVideoUrl = () => {// 直接更新 .value 触发更新videoUrl.value = 'https://example.com/new-video.mp4'
}
</script>

2. reactive vs shallowReactive

2.1 基本概念

  • reactive: 深层响应式,递归地将所有嵌套对象转换为响应式
  • shallowReactive: 浅层响应式,只将对象的第一层属性转换为响应式

2.2 使用对比

// reactive 示例
const deepState = reactive({count: 0,nested: {value: 'hello'}
})// 这些修改都会触发更新
deepState.count++  // 触发更新
deepState.nested.value = 'world'  // 触发更新// shallowReactive 示例
const shallowState = shallowReactive({count: 0,nested: {value: 'hello'}
})// 只有第一层属性的改变会触发更新
shallowState.count++  // 触发更新
shallowState.nested.value = 'world'  // 不会触发更新
shallowState.nested = { value: 'world' }  // 触发更新

2.3 实际应用示例

<template><div><h2>用户信息</h2><div>姓名: {{ userInfo.name }}</div><div>年龄: {{ userInfo.age }}</div><!-- 不需要追踪 metadata 的变化 --><div>元数据: {{ userInfo.metadata.lastUpdated }}</div></div>
</template><script setup>
import { shallowReactive } from 'vue'// 使用 shallowReactive 优化性能,metadata 的变化不需要触发更新
const userInfo = shallowReactive({name: 'John',age: 30,metadata: {lastUpdated: new Date(),visits: 0}
})// 只有顶层属性的变化会触发更新
const updateUser = () => {userInfo.name = 'Jane'  // 触发更新userInfo.metadata.visits++  // 不会触发更新
}
</script>

3. 使用场景对比

3.1 适合使用深层响应式(ref/reactive)的场景

  1. 表单数据
const formData = reactive({user: {name: '',email: '',preferences: {newsletter: true,notifications: {email: true,sms: false}}}
})
  1. 需要监听所有层级变化的数据
const settings = ref({theme: {dark: false,colors: {primary: '#000',secondary: '#fff'}}
})

3.2 适合使用浅层响应式(shallowRef/shallowReactive)的场景

  1. 大型数据结构且只需要监听顶层变化
const bigData = shallowRef({items: new Array(10000).fill(0).map((_, i) => ({id: i,data: { /* 大量数据 */ }}))
})// 整体替换数据时才触发更新
const updateData = () => {bigData.value = newBigData
}
  1. 外部库或 DOM 引用
const chartInstance = shallowRef(null)
const mapInstance = shallowRef(null)onMounted(() => {chartInstance.value = new ThirdPartyChart()mapInstance.value = new ThirdPartyMap()
})
  1. 不需要深层响应式的状态管理
const state = shallowReactive({ui: {loading: false,error: null},cache: new Map(), // 不需要响应式的缓存数据helpers: {formatter: () => {}, // 工具函数不需要响应式}
})

4. 性能优化建议

  1. 选择合适的响应式 API
// ✅ 大型数据使用浅层响应式
const bigData = shallowRef(largeDataSet)// ❌ 不必要的深层响应式
const bigData = ref(largeDataSet)
  1. 避免不必要的响应式转换
// ✅ 静态数据使用浅层响应式
const config = shallowReactive({constants: { /* 大量静态配置 */ },settings: { /* 需要响应式的设置 */ }
})// ❌ 对静态数据使用深层响应式
const config = reactive({constants: { /* 大量静态配置 */ },settings: { /* 需要响应式的设置 */ }
})
  1. 合理组合使用
// 混合使用深层和浅层响应式
const state = reactive({// 需要深层响应式的数据userSettings: {theme: 'dark',notifications: { /* ... */ }},// 使用 shallowRef 包装大型数据bigData: shallowRef(largeDataSet)
})

5. 注意事项

  1. 响应式丢失问题
const shallow = shallowReactive({nested: {count: 0}
})// 解构会失去响应性
const { nested } = shallow
nested.count++ // 不会触发更新
  1. 替换整个对象
const data = shallowRef({nested: {value: 0}
})// 需要替换整个对象才能触发更新
data.value = {nested: {value: 1}
}
  1. 与计算属性配合
// 当只需要监听部分数据变化时,使用计算属性
const data = shallowReactive({items: [],metadata: { /* ... */ }
})// 只监听 items 的变化
const computedValue = computed(() => {return processItems(data.items)
})
http://www.lryc.cn/news/527021.html

相关文章:

  • maven、npm、pip、yum官方镜像修改文档
  • HTML5+SVG+CSS3实现雪中点亮的圣诞树动画效果源码
  • HTML-新浪新闻-实现标题-样式1
  • Linux-day10
  • 【Unity3D】《跳舞的线》游戏的方块单方向拉伸实现案例
  • AI智能日志分析系统
  • 试用ChatGPT开发一个大语言模型聊天App
  • Unity Epplus读取excel表并存入So文件举例
  • 连接 OpenAI 模型:基础操作
  • [ Spring ] Spring Cloud Alibaba Message Stream Binder for RocketMQ 2025
  • ubuntu 更新24LTS中断导致“系统出错且无法恢复,请联系系统管理员”
  • 力扣-链表-203 移除链表元素
  • Unity中关于实现 管道水流+瀑布流动+大肠蠕动效果笔记
  • 宏_wps_宏修改word中所有excel表格的格式_设置字体对齐格式_删除空行等
  • Linux——网络(udp)
  • Oracle-Java JDBC 连接超时之后的认知纠正
  • 自定义数据集使用框架的线性回归方法对其进行拟合
  • 15天基础内容-5
  • 82,【6】BUUCTF WEB .[CISCN2019 华东南赛区]Double Secret
  • Android WebView 中网页被劫持的原因及解决方案
  • 特朗普政府将开展新网络攻击
  • 快递代取项目Uniapp+若依后端管理
  • arcgis短整型变为长整型的处理方式
  • 06、Redis相关概念:缓存击穿、雪崩、穿透、预热、降级、一致性等
  • 嵌入式基础 -- PCIe 控制器中断管理之MSI与MSI-X简介
  • websocket实现
  • unity学习20:time相关基础 Time.time 和 Time.deltaTime
  • 【C++】特殊类设计、单例模式与类型转换
  • scratch七彩六边形 2024年12月scratch三级真题 中国电子学会 图形化编程 scratch三级真题和答案解析
  • 代码随想录刷题day16|(哈希表篇)349.两个数组的交集