#title Weekly Vue News #90 - Optimize Performance Using shallowRef #preview Ready for your weekly Vue & Nuxt dose?
Optimize Performance Using shallowRef
Vue.js Forge is a hands-on, live coding online conference designed to deepen your knowledge of the Vue.js ecosystem and expose you to new and exciting tools and libraries rising in the ecosystem.
ποΈ Itβs all happening May 3-4! |
π€ At Vue.js Forge Episode 3 you will build an AI Social media generator! |
π¨π»βπ» Live coding sessions guided by Daniel Roe, Daniel Kelly, Boudy de Geer and other industry leaders! |
π£ This online conference is 100% FREE to attend! |
Ready to team up with thousands of fellow Vue.js coders to build and learn together?
Hi π
Last week I uploaded the video for my remote talk at Vue.js Live and a short video for Vue.js Forge Episode 3. This week I plan to relax π΄.
In this issue, I share a Vue Tip that can help you optimize performance using shallowRef. It also contains some of the best content from around the web, including articles, tutorials, and videos about Vue, Nuxt, and web development in general.
So sit back, relax, and get ready to dive into the world of Vue.js and Nuxt.js. Whether you're a developer, designer, or just someone who loves to learn about the latest tech, there's something here for you.
Have a lovely week βοΈ
To support me:
- π Recommend the newsletter to your friends: it really helps!
- πΈ Sponsor the newsletter
- π§΅ Retweet the latest Twitter thread
- π¨ Reply to this email: feedback is welcome
shallowRef
is a shallow version of
Let's start by defining a counter state using shallowRef
:
1 <script setup lang="ts">
2 import { shallowRef } from 'vue'
3
4 const state = shallowRef({
5 count: 0,
6 })
7 </script>
8
9 <template>
10 <span>Count: {{ state.count }}</span>
11 </template>
Now let's try to modify the count value:
1 <script setup lang="ts">
2 import { shallowRef, watch } from 'vue'
3
4 const state = shallowRef({
5 count: 0,
6 })
7
8 const increment = () => {
9 state.value.count = 2 // β οΈ doesn't trigger change (watcher & UI update)
10 }
11
12 watch(state, (newState) => {
13 console.log('new state', newState)
14 })
15 </script>
16
17 <template>
18 <span>Count: {{ state.count }}</span>
19 <button @click="increment">Increment count</button>
20 </template>
If you click the "Increment count" button the counter in the UI is not updated and the watcher is not fired.
Unlike ref()
, the inner value of a shallow ref is stored and exposed as-is, and will not be made deeply reactive. Only the .value
access is reactive.
The reactivity is correctly triggered if we pass a complete new value to the .value
property:
1 <script setup lang="ts">
2 import { shallowRef, watch } from 'vue'
3
4 const state = shallowRef({
5 count: 0,
6 })
7
8 const setNewValue = () => {
9 state.value = { count: 2 } // triggers change
10 }
11
12 watch(state, (newState) => {
13 console.log('new state', newState)
14 })
15 </script>
16
17 <template>
18 <span>Count: {{ state.count }}</span>
19 <button @click="setNewValue">Set new .value</button>
20 </template>
shallowRef()
is typically used for performance optimizations of large data structures, or integration with external state management systems.
Try it yourself in the following StackBlitz project:
ππ» Learn how to create custom form components with Vue.js 3 Composition API, including validation, submission handling and more.
digitalpatio.hashnode.devππ» Timi compares Nuxt vs Vitepress vs Astro |
ππ» He focuses on what they are, their key features, and their ideal use cases. |
ππ» Seven known folks of the Vue Community shared their tips on getting started with Vue.js at Vue Amsterdam 2023
youtu.beππ» A Vue.js based UnoCSS UI library including a comprehensive collection of prebuilt, ready-to-use components.
onu.zyob.topππ» createReusableTemplate |
ππ» createTemplatePromise |
ππ» useAnimate |
ππ» Improved tree-shaking |
ππ» and more... |
ππ» This article shows 10 lesser-known tips and tricks that will help you unlock TypeScript's full potential.
medium.comππ» A container query |
ππ» Scroll snap |
ππ» Grid pile |
ππ» Quick circle |
ππ» Control variants with @layer |
ππ» Memorize less and reach more with logical properties |
ππ» This article contains a transcript of a podcast featuring Douglas Crockford where he shares the story of JSON, his battles against XML, and more.
corecursive.comππ» A Prettier plugin for Tailwind CSS v3.0+ that automatically sorts classes based on their recommended class order
github.comUntil next week,
Holzapfelkreuther Str. 19, 81375 Munich, Germany