Coding recommendations
# Use getters over computed where possible
In the context of Vue.js, which is a popular JavaScript framework for building user interfaces, there are two ways to create reactive properties or values: using getters and using computed properties. Let me explain both concepts and provide examples for better understanding. Getters:
- Getters are functions that return a value based on some other reactive data. They are defined using the
ref
function. - Getters are useful when you want to calculate or manipulate a value based on other reactive properties, and you want to access the value like a normal property.
Here’s an example of using a getter:
|
|
In this example, doubleCount
is a getter that calculates the double of the count
reactive property. You can access its value by calling it like a function.
Computed Properties:
- Computed properties are values that are automatically updated whenever the reactive dependencies they rely on change. They are defined using the
computed
function. - Computed properties are useful when you want to create a new reactive value based on other reactive properties, and you want it to automatically update when those dependencies change.
Here’s an example of using a computed property:
|
|
In this example, doubleCount
is a computed property that calculates the double of the count
reactive property. You can access its value using the .value
property.
The recommendation to use getters (() => value
) over computed (computed(() => value)
) typically comes from a performance perspective. Getters are often more efficient when you only need to calculate a value once based on other reactive properties, while computed properties are better suited for values that need to automatically update whenever their dependencies change.
So, if you have a simple calculation or transformation to perform on reactive data, it’s recommended to use a getter. If you need a value that automatically updates based on reactive dependencies, then you should use a computed property.
# Setting dynamic titles
nuxt.config
does not allow the page title to be dynamic. Therefore, it is recommended to use titleTemplate
in the app.vue
file to add a dynamic title, which is then applied to all routes of your Nuxt app.
# Use your own cache keys
useAsyncData
auto-generates generates a key for cache. But the autogenerated key only takes into account the file and line where useAsyncData
is invoked, and so it is recommended to always create your own key to avoid unwanted behaviour, if you are creating your own custom composable that is wrapping useAsyncData
.
Data fetching · Get Started with Nuxt
Recommended ✅
|
|
# When to use $fetch, useFetch, useAsyncData
The useFetch
composable is meant to be invoked in setup method or called directly at the top level of a function in lifecycle hooks, otherwise you should use
$fetch
method.
Minimize payload size
Recommendation ✅
The pick
option helps you to minimize the payload size stored in your HTML document by only selecting the fields that you want returned from the composables.
|
|
If you need more control or map over several objects, you can use the transform
function to alter the result of the query.
|
|
Refresh and execute
If you want to fetch or refresh data manually, use the execute
or refresh
function provided by the composables.
|
|
Recommendation ✅
The execute
function is an alias for refresh
that works in exactly the same way but is more semantic for cases when the fetch is
not immediate.
Defining components
Using <script setup lang="ts">
is the recommended way of declaring Vue components in Nuxt 3.
# Use of Ref
Avoid ❌
Never define const state = ref()
outside of <script setup>
or setup()
function.
Such state will be shared across all users visiting your website and can lead to memory leaks!
Recommended ✅
Instead use const useX = () => useState('x')
Layout and Transition
<NuxtLayout />
renders incoming content via <slot />
, which is then wrapped around Vue’s <Transition />
component to activate layout transition. For this to work as expected, it is recommended that <NuxtLayout />
is not the root element of the page component.
|
|
Recommendation ✅
|
|
Functions called inside binding expressions will be called every time the component updates, so they should not have any side effects, such as changing data or triggering asynchronous operations.