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.
👋Hey friend! I work hard to send you amazing stuff each week.
— Michael
Lots of news recently in the Nuxt ecosystem!
First, Nuxt 4 was released!
You can read the official announcement here.
Second, last week Vercel acquired NuxtLabs (not the Nuxt framework itself) and hired Daniel Roe (lead of Nuxt team), Sébastien Chopin (creator of Nuxt) and a few others from the Nuxt team.
This is also huge news, and we covered it in our latest episode of DejaVue (scroll down a bit to find it).
I'm working through my own thoughts on this and will have an article soon. But if you want more context, here are a bunch of links to check out:
Also, check out this amazing video showing how Nuxt 4 was released.
I've also got the regular tips and such here.
Enjoy your week!
— Michael
Testing is important to do, but it can be hard to do.
In my experience, good architecture lends itself to easy-to-write tests (or at least, easier-to-write). The inverse is also true, that difficult-to-write tests are typically a symptom of poor architecture.
Of course, sometimes tests are just hard to write, and there’s no way around it.
The best thing we can do is borrow a tool from mathematics and science, and transform a difficult problem into an easier but equivalent one:
We can add computed and methods directly on to a reactive object:
const counter = reactive({count: 0,increment() {this.count += 1;},decrement() {this.count -= 1;},});
This works because this
is set to the object that the method is accessed through, which happens to be the reactive object.
Vue’s reactivity system uses Proxies to watch for when a property is accessed and updated. In this case, we have a small overhead from accessing the method as a property on the object, but it doesn’t trigger any updates.
If we had a whole series of counters we can reuse this over and over:
const listOfCounters = [];for (const i = 0; i < 10; i++) {const counter = reactive({id: i,count: 0,increment() {this.count += 1;},decrement() {this.count -= 1;},})listOfCounters.push(counter);}
In our template we can use the counters individually:
<div v-for="counter in listOfCounters" :key="counter.id"><button @click="counter.decrement()">-</button>{{ counter.count }}<button @click="counter.increment()">+</button></div>
Instead of making the entire object reactive, we can use ref
to make only our state reactive:
const counter = {count: ref(0),increment() {this.count.value += 1;},decrement() {this.count.value -= 1;},};
This saves us a small and likely unnoticeable overhead. But it also feels somewhat better since we’re being more thoughtful with our use of reactivity instead of spraying it everywhere.
Here’s our example from before, but this time I’m going to add in a factory function to make it more readable:
const createCounter = (i) => ({id: i,count: ref(0),increment() {this.count.value += 1;},decrement() {this.count.value -= 1;},});const listOfCounters = [];for (const i = 0; i < 10; i++) {listOfCounters.push(createCounter(i));}
Of course, we can use a factory method with the previous reactive method as well.
The <script setup>
sugar in Vue 3 is a really nice feature, but did you know you can use it and a regular <script>
block?
<script setup>// Composition APIimport { ref } from 'vue';console.log('Setting up new component instance');const count = ref(0);</script><script>// ...and the options API too!export default {name: 'DoubleScript',};</script>
This works because the <script setup>
block is compiled into the component's setup()
function.
There are a few reasons why you might want to do this:
inheritAttrs
. For these you can also use defineOptions
.setup()
is run for every component, if you have code that should only be executed once, you can't include it in <script setup>
. You can put it inside the regular <script>
block, though.<script>
block.Check out the docs for more info
In this special episode of DejaVue, Alexander and Michael are joined by Daniel Roe and Sébastien Chopin to discuss the recent acquisition of NuxtLabs by Vercel. Questions like "Was Nuxt just sold", "How much influence has Vercel", and "What is Vercel excepting from the deal" are answered.
If you wonder what impact the deal has on Nuxt, you as a user and developer, as well as the open-source community, you should tune in!
Enjoy the Episode!
Watch on YouTube or listen on your favorite podcast platform.
In case you missed them:
Prose components are perhaps one of the best features of Nuxt Content!
In this article, we'll go through a few examples of how you can easily create your own.
Check it out here: Mastering Prose Components in 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
"First do it, then do it right, then do it better." — Addy Osmani
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.
Props down, events up. That's how your components should communicate — most of the time.
But in rare cases, that just doesn't work.
If you need direct access to the parent component, you should just use provide
/inject
to pass down the relevant value or method:
import { provide } from 'vue';const someMethodInTheParent = () => {};provide('method', someMethodInTheParent)
Then, inject it into the child component:
import { inject } from 'vue';const method = inject('method');method();
In Vue 2, you can also use the instance property $parent
:
// Tight coupling like this is usually a bad ideathis.$parent.methodOnParentComponent();
This is simpler, but leads to higher coupling and will more easily break your application if you ever refactor.
You can also get direct access to the application root, the very top-most component in the tree, by using $root
. Vue 2 also has $children
, but these were taken out for Vue 3 (please don't use this one).
When would these be useful?
There are a few different scenarios I can think of. Usually, when you want to abstract some behaviour and have it work "magically" behind the scenes.
You don't want to use props and events to connect up a component in those cases. Instead, you use provide
/inject
, $parent
, or $root
, to automatically connect the components and make things happen.
(This is similar to the Compound Component pattern)
But it's hard to come up with an example where this is the best solution. Using provide
/inject
is almost always the better choice.
Michael Hoffman curates a fantastic weekly newsletter with the best Vue and Nuxt links.
p.s. I also have a bunch of products/courses: