👉🏻 This "Quick Start with Pinia" lesson on Pinia breaks down this powerful Vue state management library, showing you how to efficiently manage data across components.
👉🏻 This library is written using Vue 3 with Composition API, and the components are designed and written in a traditional Vue way possible, with full TypeScript support.
👉🏻 Lightweight and framework independent but offers Vue 2/3, Alpine.js and Svelte integrations.
🔥 Vue Tip: Avoid Side Effects in Computed Properties
It is considered bad practice to introduce side effects inside computed properties and functions because it makes the code unpredictable and hard to understand.
What is a side effect? In the context of computed properties, a side effect is a modification of the state's global state or the internal component state.
Let's take a look at an example with side effects:
1<script setup lang="ts">2import { computed, ref } from 'vue'34const firstName = ref('Michael')5const lastName = ref('Hoffmann')6const array = ref([])78const fullName = computed(() => {9 firstName.value = 'Mike' // side effect10 return `${firstName.value} ${lastName.value}`11})1213const reversedArray = computed(() => array.value.reverse()) // side effect, original array is mutated14</script>
Now let's change the code and remove the side effects:
1<script setup lang="ts">2import { computed, ref } from 'vue'34const firstName = ref('Michael')5const lastName = ref('Hoffmann')6const array = ref([])78const fullName = computed(() => `${firstName.value} ${lastName.value}`)9const reversedArray = computed(() => array.value.slice(0).reverse()) // .slice creates a copy of the array10</script>
Now the code is predictable and easy to understand. The computed properties fullNamefullName and reversedArrayreversedArray only depend on the values of firstNamefirstName, lastNamelastName, and arrayarray. They don't modify the global state or the internal component state.
Weekly Vue News #194 Reactive Time Ago View online Hi 👋 I'm on vacation this week, so no special news from my side — just some fresh Vue & Nuxt content for you! Enjoy this issue and have a lovely week ☀️ Vue 📕 Optimizing heavy operations in Vue with Web Worke...
Hey! In yesterday's email I shared what I think is the key feature to making Vue components highly reusable: Scoped slots. But scoped slots are hard to grasp, and even more difficult to master. So today, we're going to make sure we understand them on a deep, intuitive level. Then, I'm going to introduce you to the magic ✨ of scoped slots. The trick is to think of them as functions. Slots are just functions We're going to recreate the functionality of slots, but we'll use a regular Javascript function that only returns HTML. This is the code we'll replicate: <!-- Parent --> < template > < div class = "modal-container" > < div class = "modal" > Content in the Parent < Child class = "mb-4" v-slot = "{ text }" > ...
评论
发表评论