Hey there!
Today I launched Advanced Reactivity!
If you missed it, you can check it out here: Advanced Reactivity.
I recently wrote two articles based on the course:
Enjoy your tips and the rest of the newsletter!
— Michael
The simplest solution to lots of state management problems is to use a composable to create a shareable data store.
This pattern has a few parts:
Here's a simple example:
import { reactive, toRefs, readonly } from 'vue';import { themes } from './utils';// 1. Create global state in module scope, shared every// time we use this composableconst state = reactive({darkMode: false,sidebarCollapsed: false,// 2. This theme value is kept private to this composabletheme: 'nord',});export default () => {// 2. Expose only some of the state// Using toRefs allows us to share individual valuesconst { darkMode, sidebarCollapsed } = toRefs(state);// 3. Modify our underlying stateconst changeTheme = (newTheme) => {if (themes.includes(newTheme)) {// Only update if it's a valid themestate.theme = newTheme;}}return {// 2. Only return some of the statedarkMode,sidebarCollapsed,// 2. Only expose a readonly version of statetheme: readonly(state.theme),// 3. We return a method to modify underlying statechangeTheme,}}
Scoped CSS is fantastic for keeping things tidy and not accidentally bleeding styles into other parts of your app.
But sometimes, you need to override the styles of a child component and break out of that scope.
Vue has a :deep selector just for this:
<style scoped>/* Override CSS of a child componentwhile keeping styles scoped */.my-component :deep(.child-component) {font-size: 24px;}</style>
In Vue 2 this has a slightly different syntax depending on which CSS pre-processor you're using:
<style scoped>/* When using SASS */.my-component ::v-deep .child-component {font-size: 24px;}/* Everything else */.my-component >>> .child-component {font-size: 24px;}</style>
Yes, I have previously covered why you shouldn't do this, but overriding styles can be the best solution (we don't believe in "best practices" here).
One handy helper method in @nuxt/test-utils is mockNuxtImport.
It's a convenience method to make it easier to mock anything that Nuxt would normally auto-import:
import { mockNuxtImport } from '@nuxt/test-utils/runtime';mockNuxtImport('useAsyncData', () => {return () => {return { data: 'Mocked data' };};});// ...tests
So far in this series we’ve covered a lot on how to use Prisma in our Nuxt apps.
But we’ve left out a major piece — actually being able to modify the data in the database.
A pretty crucial part of the puzzle I think!
In this article I'll show you how to modify data in your database using Prisma.
Check it out here: Prisma with Nuxt: Modifying Data with Prisma (5 of 5)
There are a lot of different ways to redirect users in Nuxt, each with their own use cases.
In this (very detailed!) article I explore the different ways to redirect users in Nuxt, giving examples of when each might be useful.
Check it out here: How to Redirect in Nuxt (Every Single Way)
"When in doubt, use brute force." — Steve McConnell
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.
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
Michael Hoffman curates a fantastic weekly newsletter with the best Vue and Nuxt links.
p.s. I also have a bunch of products/courses: