Exclusive tips every week

If you like Anthony's newsletter on Vue.js Developers, you'll enjoy this one, too.

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

    🔥 (228) Optimize for humans, reassigning template refs, and Vue to Web Component

    Hi there!

    I'm on vacation this week (this was scheduled ahead of time) so I don't have anything new to share.

    However, if you're interested in Mastering Nuxt: Full Stack Unleashed, the price is going up in a couple days so make sure to check it out if you're thinking about it!

    Here's the link: Mastering Nuxt: Full Stack Unleashed

    Other than that, enjoy your week and your tips!

    — Michael

    🔥 Optimize for Humans

    The most important thing we can do when writing code is to make it work.

    The second most important thing is to make it understandable to other humans — including ourselves.

    All too often we write clever code, terse code, code that isn’t even understandable to ourselves when we come back to it a week or a month later.

    Here are some ways to fix that:

    • Extract Components — by replacing a chunk of code with a meaningful name, we can separate the intention of the code from the implementation of the code. Good names are the most valuable form of abstraction that we have.
    • Shorter components — longer components are harder to understand at a glance, and that should be the ideal we strive for. The harder it is to understand a single component, the more mistakes you’ll make, and the longer it will take for you implement anything new.
    • Optimize for the most tired, frustrated version of yourself — Remember that we all have bad days, and we want to productive every day, not just our best days. Write code that even the worst version of you can understand.

    🔥 Reassigning Template Refs

    Reassigning values can cause issues when using the simplest form of template refs:

    <template>
    <div>
    <h1 ref="heading">This is my page</h1>
    </div>
    </template>

    In this case, we can’t use a reactive object at all:

    const heading = reactive(null);
    watchEffect(() => console.log(heading));
    // "null"

    When the component is first instantiated, this will log out null, because heading has no value yet. But when the component is mounted and our h1 is created, it will not trigger.

    The heading object becomes a new object, and our watcher loses track of it. The reference to the previous reactive object is overwritten.

    We need to use a ref here:

    const heading = ref(null);
    watchEffect(() => console.log(heading.value));
    // "null"

    This time, when the component is mounted it will log out the element. This is because only a ref can be reassigned in this way.

    It is possible to use reactive in this scenario, but it requires a bit of extra syntax using function refs:

    <template>
    <div>
    <h1
    :ref="(el) => { heading.element = el }"
    >
    This is my page
    </h1>
    </div>
    </template>

    Then our script would be written as so, using the el property on our reactive object:

    const heading = reactive({ el: null });
    watchEffect(() => console.log(heading.el));
    // "null"

    🔥 Vue to Web Component in 3 Easy Steps

    Here's how you can create web components in Vue.

    First, create the custom element from a Vue component using defineCustomElement:

    import { defineCustomElement } from 'vue';
    import MyVueComponent from './MyVueComponent.vue';
    const customElement = defineCustomElement(MyVueComponent);

    Second, register the custom element with the DOM:

    customElements.define('my-vue-component', customElement);

    Third, use the custom element in your HTML:

    <html>
    <head></head>
    <body>
    <my-vue-component></my-vue-component>
    </body>
    </html>

    Now you've got a custom web component that doesn't need a framework and can run natively in the browser!

    Check out the docs for more details on how this works.

    📜 3 Ways to Create Inline Composables

    Composables are great, except that it seems we always need to create a new file for them.

    In this article I explore some ways we can create inline composables — no need to create new files all over the place!

    Check it out here: 3 Ways to Create Inline Composables

    📜 Build 3D Scenes Declaratively with TresJS Using Vue

    Alvaro has done some really impressive work with TresJS, a 3D library for Vue.

    In this article he showcases just how easy it is to create 3D scenes in Vue when using TresJS.

    Definitely check it out if you're interested in 3D development with Vue!

    Check it out here: Build 3D Scenes Declaratively with TresJS Using Vue

    đź’¬ The Code You Don't Write

    "The code you write makes you a programmer. The code you delete makes you a good one. The code you don't have to write makes you a great one." — Mario Fusco

    đź§  Spaced-repetition: toRef default value

    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.

    You've been using toRef for a while, but did you know you can also supply a default value?

    const bank = reactive({
    Rand: 3400,
    Egwene: 20,
    Matrim: 230340,
    Padan: -20340,
    })
    // toRef(object, property, default)
    const myBankAccount = toRef(bank, 'Michael', 1000 * 1000);

    Probably the easiest way to become a millionaire.

    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.