#title Weekly Vue News #88 - Validate Events and Prop Types #preview Ready for your weekly Vue & Nuxt dose?
Validate Events and Prop Types
There're 2 approaches to adding a reactive state to Vue components using the Composition API. And you must choose whether to utilize reactive(), ref(), or both.
In this article, I'll explain how you can choose whether to utilize reactive, ref, or both.
Hi π
I'm still busy working on my talk for Vue.js Live 2023. As a preparation, I wrote an article about the topic "Ref vs. Reactive: What to Choose Using Vue 3 Composition API?" which is linked at top of this issue.
This issue is packed full of amazing articles about Vue, Nuxt and Web Development in general. I hope you enjoy it!
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
You can validate prop types and events in Vue. If a requirement is not met, Vue will warn you in the browser's JavaScript console.
Such validations are typically used if your component is intended to be used by others.
Prop Type Validation
In <script setup>
, only the function argument of defineProps()
supports custom validator functions (as of Vue 3.2.31).
To specify a prop validation function, you can provide an object with validation requirements to the defineProps()
macro:
1 <script setup>
2 defineProps({
3 type: {
4 type: String,
5 default: 'success',
6 validator(value) {
7 // The value must match one of these strings
8 return ['success', 'warning', 'danger'].includes(value)
9 },
10 },
11 })
12 </script>
Let's pass an invalid type to the component:
1 <template>
2 <Component type="invalid" />
3 </template>
Vue will produce a console warning (if using the development build):
Invalid prop: custom validator check failed for prop "type"
Events Validation
It's also possible to validate an emitted event.
You need to assign a function that receives the emit call's arguments. The function returns a boolean to indicate whether the event is valid or not:
1 <script setup>
2 const emit = defineEmits({
3 // No validation
4 click: null,
5
6 // Validate update event
7 update: (value) => {
8 if (value) {
9 return true
10 } else {
11 console.warn('Invalid value event payload!')
12 return false
13 }
14 },
15 })
16 </script>
17
18 <template>
19 <button @click="$emit('update')">Update</button>
20 </template>
StackBlitz Example
You can try it yourself in the following StackBlitz project:
ππ» This article looks at the newest version of Nuxt DevTools, a visual tool for understanding Nuxt apps. |
ππ» Nuxt DevTools was created to help developers find performance bottlenecks and help manage apps and configurations. |
ππ» Handling migrations within long-term projects can be tough, and this is an interesting deep dive into how one team did it.
www.smashingmagazine.comππ» Michael shows how to use Supabase in a Nuxt 3 middleware to block users who arenβt logged in.
masteringnuxt.comππ» This article will explore the differences between watch and watchEffect and when to use each.
fadamakis.comππ» Akanni takes a look at Vuetify 3 and "how it compares to its former counterpart, and get clear on the new features as well as breaking changes".
medium.comππ» Dmitri explains use v-model to bind form inputs in Vue 3.
dmitripavlutin.comππ» Elian explains in this article how Vue developers can get started with Astro. |
ππ» Astro is a new content focused website builder that focuses on shipping less JavaScript. |
ππ» TheΒ SonnerΒ toast component but for Vue. |
ππ» It's customizable, but styled by default. |
ππ» Comes with a swipe to dismiss animation. |
ππ» βThe single best feature of Vite, as far as Iβm concerned, is its simplicity. Compared to the nightmare of configuring WebPack and Babel? Vite is delightfully easy toΒ use.β
cloudfour.comThis article gives solutions for the following GitHub Actions problems: |
ππ» Dependency caching |
ππ» Cancelation of stale executions |
ππ» Path filtering |
ππ» Timeouts |
ππ» This library provides a set of composable functions for the type-level that enable developers to transform TypeScript types using familiar functions.
github.comππ» Responsively highlight search results within a textarea element without interfering with its operation |
ππ» Live Demo: https://wstaeblein.github.io/texthighlighter/ |
Until next week,
Holzapfelkreuther Str. 19, 81375 Munich, Germany