Exclusive tips every week

Join 13,567+ other Vue devs and get exclusive tips and insights delivered straight to your inbox, every week.

    Picture of Michael Thiessen

    πŸ‘‹Hey friend! I work hard to send you amazing stuff each week.

    β€”Β Michael

    I really love and enjoy reading these emails.

    You are one of the most pro VueJS devs I know, and I am happy that you share this knowledge.

    Fabian Beer

    Here's my latest newsletter

    πŸ”₯ (249) Template Props to Slots, SSR Safe Directives, and When to Use v-if

    Hey!

    Merry Christmas Eve! I hope you're having a wonderful time today, whether you're celebrating with loved ones or enjoying some well-deserved downtime.

    A few tips and articles to browse if you have some quiet time today.

    β€” Michael

    πŸ”₯ Improve reusability by converting template props into slots

    One kind of prop, a template prop, can be directly converted into slots without very much work.

    This makes your component more reusable.

    The text prop here is a template prop, because it is only ever used in the template:

    <template>
    <button @click="$emit('click')">
    {{ text }}
    </button>
    </template>
    <script setup>
    const props = defineProps({
    text: {
    type: String,
    required: true,
    },
    });
    defineEmits(['click']);
    </script>

    It doesn't get used in any calculations or passed as a prop anywhere. Instead, it just gets directly interpolated and rendered to the page.

    These props can be directly replaced with slots:

    <template>
    <button @click="$emit('click')">
    <slot />
    </button>
    </template>
    <script setup>
    defineEmits(['click']);
    </script>

    This sort of cleans up the code, but more importantly, it allows us to be more flexible with how the component can be used.

    With a prop we have to use the component like this:

    <Button text="Click me" @click="handleClick" />

    But with a slot, we can add in whatever we want:

    <Button @click="handleClick">
    Click on <strong>this</strong> button
    </Button>

    πŸ”₯ SSR Safe Directives

    In many cases, we need to generate unique IDs for elements dynamically.

    But we want this to be stable through SSR so we don't get any hydration errors.

    And while we're at it, why don't we make it a directive so we can easily add it to any element we want?

    Here's a stripped-down version of this directive:

    const generateID = () => Math.floor(Math.random() * 1000);
    const directive = {
    getSSRProps() {
    return { id: generateID() };
    },
    }

    When using it with Nuxt, we need to create a plugin so we can register the custom directive:

    // ~/plugins/dynamic-id.ts
    const generateID = () => Math.floor(Math.random() * 1000);
    export default defineNuxtPlugin((nuxtApp) => {
    nuxtApp.vueApp.directive("id", {
    getSSRProps() {
    return { id: generateID() };
    },
    });
    });

    In Nuxt 3.10+, you can also use the useId composable instead:

    <template>
    <div :id="id" />
    </template>
    <script setup>
    const id = useId();
    </script>

    Normally, custom directives are ignored by Vue during SSR because they typically are there to manipulate the DOM. Since SSR only renders the initial DOM state, there's no need to run them, so they're skipped.

    But there are some cases where we actually need the directives to be run on the server, such as with our dynamic ID directive.

    That's where getSSRProps comes in.

    It's a special function on our directives that is only called during SSR, and the object returned from it is applied directly to the element, with each property becoming a new attribute of the element:

    getSSRProps(binding, vnode) {
    // ...
    return {
    attribute,
    anotherAttribute,
    };
    }

    πŸ”₯ When should you use v-if?

    Instead of using v-if, it's sometimes more performant to use v-show instead:

    <ComplicatedChart v-show="chartEnabled" />

    When v-if is toggled on and off, it will create and destroy the element completely. Instead, v-show will create the element and leave it there, hiding it by setting its style to display: none.

    Doing this can be much more efficient if the component you're toggling is expensive to render.

    On the flip side, if you don't need that expensive component immediately, use v-if so that it will skip rendering it and load the page just a bit faster.

    πŸ“œ Nuxt Extends (video)

    In this video from LearnVue, we see how powerful the extends feature is in Nuxt.

    Check it out here: Nuxt Extends (video)

    πŸ“œ Dynamically Updating my Landing Page with Nuxt Content

    I recently spent some time updating the landing page for Clean Components Toolkit so that it will automatically update the outline as I update the course content itself.

    In this article, I'll show you how it's done.

    Check it out here: Dynamically Updating my Landing Page with Nuxt Content

    πŸ’¬ Carpentry vs. Software

    "In carpentry you measure twice and cut once. In software development you never measure and make cuts until you run out of time." β€” Adam Morse

    🧠 Spaced-repetition: Static and dynamic classes

    The best way to commit something to long-term memory is to periodically review it, gradually increasing the time between reviews πŸ‘¨β€πŸ”¬

    Actually remembering these tips is much more useful than just a quick distraction, so here's a tip from a couple weeks ago to jog your memory.

    We can add static and dynamic classes to an element at the same time:

    <ul>
    <li
    v-for="item in list"
    :key="item.id"
    class="always-here"
    :class="{ selected: item.selected }"
    >
    {{ item.name }}
    </li>
    </ul>

    This lets you apply basic styling through static classes and then dynamically add other styles as you need them.

    You can also achieve the same thing when using an Object or Array with dynamic classes:

    <ul>
    <li
    v-for="item in list"
    :key="item.id"
    :class="{
    'always-here': true,
    selected: item.selected,
    }"
    >
    {{ item.name }}
    </li>
    </ul>

    Or with an Array:

    <ul>
    <li
    v-for="item in list"
    :key="item.id"
    :class="[
    'always-here',
    item.selected && 'selected',
    ]"
    >
    {{ item.name }}
    </li>
    </ul>

    I prefer splitting them out into class and :class bindings though, since it makes the code clearer. It also makes it less likely to be broken when refactored!

    Michael Hoffman curates a fantastic weekly newsletter with the best Vue and Nuxt links.

    Sign up for it here.

    p.s. I also have a bunch of products/courses:

    Here's what others are saying

    I'm starting to think that your newsletter is one of the best things that happened to me this year. I just love these emails.
    Stanislaw Gregor
    I'm somewhere in the upper-middle level at Vue, and I never miss an email you and always find something cool when reading!
    Eduard Climov
    This is the first time where I'm actually enjoying email newsletters. I like your format a lot.
    Fahmi Alfituri
    You have great content in your emails. I seriously learn something from every one of them.
    Titus Decali
    Just wanted to say I enjoy these emails. They encourage me to constantly improve my craft. Fantastic work.
    Joe Radman
    Thanks for another beautiful tip πŸ™
    Victor Martins Onuoha
    Loving these, and the spaced repetition.
    Mark Goldstein
    I really enjoy reading your emails, because I love Vue. Thanks for these emails.
    Arturo Espinoza
    I really love and enjoy reading these emails. You are one of the most pro VueJS devs I know, and I am happy that you share this knowledge.
    Fabian Beer
    THANK YOU! I did not know about the deep property, so I assumed you simply couldn't watch objects.
    Darryl Noakes
    I really must say you are doing a really great job. Now I am aware of a cleaner and more performant way to use Tailwind. Thanks a lot!
    Henry Eze
    Thank you so much, I really enjoy and appreciate your emails.
    Carlos Gonzalez
    Thanks for sharing great Vue tips.
    Fernando Navarro
    I really enjoy these tips.
    Martin H
    Thank you so much for the weekly Vue education. Thanks and live on longer to educate us more.
    Kabolobari Benakole
    I look forward to your emails every week. This week was something I knew, but I like to be reminded of. Thanks for keeping it up!
    Nathan Strutz
    Thank you for your weekly emails. I always look forward to learning a new tip about Vue or at least relearning old tips :)
    Colin Johnson
    I have really been enjoying your weekly emails, and I also got my two team members to join in on the fun as well.
    Keith Dawson
    Thank you for your newsletter, your explanations have very concise examples and I like it.
    Nicolas Decayeux
    Thanks A LOT for your great work!!! One of the few newsletters that I let pass!
    Martin Tillmann

    Want to level up your Vue skills?

    With over two million reads and 11,067 subscribers, you've come to the right place.

    Subscribe now to get exclusive insights and tips every week.