---
title: "7 Vue3 tips & tricks I guarantee you didn’t know"
description: "Vue3 has a lot of hidden gems. Here are seven tips and tricks that most developers don't know about — but definitely should."
canonical_url: "https://rolandi.dev/blog/vue3-tips-tricks"
last_updated: "2026-09-21T17:32:28.520Z"
---

Vue 3 has a lot of hidden gems. Here are seven tips and tricks that most developers don't know about — but definitely should.

## 1. VNode hooks (v3.3+)

On any component or HTML tag, we can use some special hooks as event listeners. The hooks are:

- `@vue:beforeMount`
- `@vue:mounted`
- `@vue:beforeUpdate`
- `@vue:updated`
- `@vue:beforeUnmount`
- `@vue:unmounted`

Beaware that these hooks are not documented in the official Vue 3 docs which means that they are not officially supported. So use them at your own risk.

```vue
<script setup>
import { ref } from 'vue'
import Comp from './Comp.vue'

const count = ref(0)
const cmp = ref(true)

function onMyComponentMounted() { console.log('component mounted') }
function onMyComponentUnMounted() { console.log('component unmounted') }
function divThatDisplaysCountWasUpdated() { console.log('div was updated') }
</script>

<template>
  <p>Check the console in devtools</p>
  <hr />
  <Comp v-if="cmp" @vue:mounted="onMyComponentMounted" @vue:unmounted="onMyComponentUnMounted" />
  <div @vue:updated="divThatDisplaysCountWasUpdated">{{ count }}</div>

  <button @click="count++">+</button>
  <button @click="cmp = !cmp">Mount/Unmount component</button>
</template>
```

It should be noted that these hooks pass arguments to the callback function. They pass only one argument — the current VNode — except for `@vue:beforeUpdate` and `@vue:updated`, which pass two arguments: the current VNode and the previous VNode.

## 2. Debugging hooks

We all know the lifecycle hooks that Vue provides us. But did you know that Vue 3 gives us two hooks we can use for debugging purposes? They are:

- [onRenderTracked](https://vuejs.org/api/composition-api-lifecycle.html#onrendertracked)
- [onRenderTriggered](https://vuejs.org/api/composition-api-lifecycle.html#onrendertriggered)

`onRenderTracked` gets called for every reactive dependency that has been tracked.

```vue
<script setup>
import { ref, onRenderTracked } from 'vue'

const count = ref(0)
const count2 = ref(0)

// It will be called for every reactive dependency that has been tracked
onRenderTracked((event) => {
  console.log(event)
})
</script>
```

`onRenderTriggered` gets called when we trigger a reactivity update, or as the docs say: "when a reactive dependency triggers the component's render effect to be re-run".

```vue
<script setup>
import { ref, onRenderTriggered } from 'vue'

const count = ref(0)

// It will be called when we update the reactive dependency
onRenderTriggered((event) => {
  debugger
})
</script>
```

## 3. Expose slots from a child component

If you use a third-party component, chances are that you wrap its implementation in your own "wrapper" component. This is a good practice and a scalable solution, but in that way the slots of the third-party component get lost, and we should find a way to expose them to the parent component:

```vue
<!-- WrapperComponent.vue -->
<template>
  <div class="wrapper-of-third-party-component">
    <ThirdPartyComponent v-bind="$attrs">
      <!-- Expose the slots of the third-party component -->
      <template v-for="(_, name) in $slots" #[name]="slotData">
        <slot :name="name" v-bind="slotData || {}"></slot>
      </template>
    </ThirdPartyComponent>
  </div>
</template>
```

Now every component that uses `WrapperComponent` can use the slots of `ThirdPartyComponent` 🎉.

## 4. Scoped styles and multi-root nodes don't work well together

In Vue 3 we can finally have more than one root node in a component. That is great, but personally I fall into a design limitation when doing that. Imagine we have a child component:

```vue
<template>
  <p class="my-p">First p</p>
  <p class="my-p">Second p</p>
</template>
```

And a parent component:

```vue
<template>
  <h1>My awesome component</h1>
  <MyChildComponent />
</template>

<style scoped>
/* There is no way to style the p tags of MyChildComponent */
.my-p { color: red; }
:deep(.my-p) { color: red; }
</style>
```

There is no way from the scoped styling of the multi-root parent component to style the child component's `p` tags. In short: a multi-root component can't target a multi-root child component's styles with scoped styles.

The best way to fix that would be to wrap the parent or child component (or both) so we have only one root element. But if you absolutely need both to have multi-root nodes, you can:

**Use a non-scoped style**

```vue
<style>
.my-p { color: red; }
</style>
```

**Use CSS Modules**

```vue
<template>
  <h1>My awesome component</h1>
  <MyChildComponent :class="$style.trick" />
</template>

<style module>
.trick {
  color: red;
}
</style>
```

Since we are specifying a class here, the multi-root child component has to explicitly specify the attribute fallthrough behavior.

If you want my opinion: unless you absolutely need a multi-root node component, go with a single root node and don't deal with this design limitation at all.

## 5. Be careful when using CSS selectors

`#main-nav > li {}` will be many times slower compared to `.my-li { color: red }`. From the docs:

> Due to the way browsers render various CSS selectors, `p { color: red }` will be many times slower when scoped (i.e. when combined with an attribute selector). If you use classes or ids instead, such as in `.example { color: red }`, then you virtually eliminate that performance hit.

I highly recommend you read [Efficiently Rendering CSS](https://css-tricks.com/efficiently-rendering-css/) if you want to dive deeper into this topic.

## 6. Boolean casting

In Vue 2 or early versions of Vue 3, for props with `Boolean` types we had different behavior depending on the order:

```js
// 1st case
props: {
  hoverColor: [String, Boolean] // <- defaults to ''
}

// 2nd case
props: {
  hoverColor: [Boolean, String] // <- defaults to false
}
```

Not only that, but if you pass the prop like this:

```vue
<my-component hover-color></my-component>
```

In the first case it would be an empty string `''`. In the second case, it would be `true`.

As you can see, this was a bit confusing and inconsistent. Fortunately, in Vue 3 we have a new behavior that is consistent and predictable:

> `Boolean` behavior will apply regardless of type appearance order.

So:

```js
hoverColor: [String, Boolean] // <- defaults to false
hoverColor: [Boolean, String] // <- defaults to false
hoverColor: [Boolean, Number] // <- defaults to false
```

## 7. Template refs with v-for — order is not guaranteed

Remember this one so you don't lose hours of debugging trying to figure out what is going on.

In the code below:

```vue
<script setup>
import { ref } from 'vue'

const list = ref([1, 2, 3])
const itemRefs = useTemplateRef('items')
</script>

<template>
  <ul>
    <li v-for="item in list" ref="items" :key="item">
      {{ item }}
    </li>
  </ul>
</template>
```

we are looping over the `list` array and creating the `itemRefs` array. **The itemRefs array is not guaranteed to have the same order as the list array.**

If you want to find out more about this, you can read [this issue](https://github.com/vuejs/core/issues/4010).

## Wrapping up

Thank you all for reading. Please share this post with your friends and colleagues if you found it useful.

If you haven't already, follow me on [Linkedin](https://www.linkedin.com/in/roland-doda/)
