7 Vue3 tips you need to know
Tags: vue
Published: 2022-09-17 16:08:11

 - [VNode hooks](#vnode-hooks)
 - [Debugging hooks](#debugging-hooks)
 - [Expose slots from a child component](#expose-slots-from-a-child-component)
 - [Scoped styles and multi-root nodes don't work well together](#scoped-styles-and-multi-root-nodes-dont-work-well-together)
 - [Be careful when using CSS selectors](#be-careful-when-using-css-selectors)
 - [Boolean casting](#boolean-casting)
 - [Template refs with v-for - order is not guaranteed](#template-refs-with-v-for-order-is-not-guaranteed)

## VNode hooks <a name="vnode-hooks"></a>

On each component or HTML tags, we can use some special (undocumented) hooks as event listeners. The hooks are:

-  `onVnodeBeforeMount`
-  `onVnodeMounted`
-  `onVnodeBeforeUpdate`
-  `onVnodeUpdated`
-  `onVnodeBeforeUnmount`
-  `onVnodeUnmounted`

I mainly use `onVnodeMounted` on components when I need to execute some code when the component is mounted or `onVnodeUpdated` to debug when something is updated but I am quite sure all of them can come in handy in some cases.

*Example:*
```html
<script setup>
    import { ref } from 'vue'

    const count = ref(0)

    function onMyComponentMounted() {}

    function divThatDisplaysCountWasUpdated() {}
</script>

<template>
    <MyComponent @vnodeMounted="onMyComponentMounted" />
    <div @vnodeUpdated="divThatDisplaysCountWasUpdated">{{ count }}</div>
</template>
```

It should be noted, that these hooks pass some arguments to the callback function. They pass only one argument which is the current VNode except for `onVnodeBeforeUpdate` and `onVnodeUpdated` that pass two arguments, the current VNode and the previous VNode.

## Debugging hooks <a name="debugging-hooks"></a>

We all know the lifecycle hooks that Vue provides us. But did you know that Vue 3 gives us two hooks that 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. 

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

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

// It will be called twice, once for count and once for count2
onRenderTracked((event) => {
    console.log(event)
})
</script>
```

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

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

const count = ref(0)

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

## Expose slots from a child component <a name="expose-slots-from-a-child-component"></a>

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

*WrapperComponent.vue*
```html
<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` 🎉.


## Scoped styles and multi-root nodes don't work well together <a name="scoped-styles-and-multi-root-nodes-dont-work-well-together"></a>

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

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


And a parent component:

```html
<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. 

> So in short, a multi-root component, can't target 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

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

- Use CSS Modules

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

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

Since we are specifying a class here, then 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.

## Be careful when using CSS selectors <a name="be-careful-when-using-css-selectors"></a>

`#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 to read [Efficiently Rendering CSS](https://css-tricks.com/efficiently-rendering-css/) if you want to dive deeper into this topic.

## Boolean casting <a name="boolean-casting"></a>

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

1st case:

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

2nd case:

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

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

```html
<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
```

## Template refs with v-for - Order is not guaranteed <a name="template-refs-with-v-for-order-is-not-guaranteed"></a>

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

In the code below:

```html
<script setup>
import { ref } from "vue";

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

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

we are looping over the `list` array, and we create the `itemRefs` array.

**`itemRefs` is not guaranteed to have the same order as `list` array.**

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

## Ending the article

*I do have more tips and tricks, but this article became long already. Expect another article in the near future with even better tips/tricks.*

Thank you all for reading, any feedback is highly appreciated!

### Know a cool company hiring Vue.js developers?

Also, I am looking for a new job, so if you know a cool company that is hiring a Senior Vue.js developer, please reach out to rolanddoda2014@gmail.com 🙏.

-------------
-------------

<a href="https://www.buymeacoffee.com/rolanddoda" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" height="41" width="174"></a>