From my notes

Reusability

Questions and flashcards from your notes on the Vue docs' Reusability chapter — composables with shared and per-caller state, conventions, toValue and MaybeRefOrGetter inputs, lifecycle hooks in composables, custom directives and their bindings, global directives, plugins and a small i18n plugin — with a fact check of the notes.

Written from your composables, directives and plugins note. The matching study page is Reusability — composables, directives, plugins.

Know

  • Composables: module-level versus per-caller state, the naming and return conventions, toValue() with refs, getters and plain values, and why lifecycle hooks in composables only work during setup.
  • Directives: naming in <script setup>, when a directive is the right tool, the function shorthand, the binding object, and why directives on components are discouraged.
  • Plugins: what app.use() calls, what a plugin can configure, and an i18n plugin that provides a function and a template helper.

Fact check of your notes

This note is accurate. Two small things to tidy.

Gotcha· The directives warning sits under composables

"Not recommended for use on custom components" is listed under composable best practices, but it is a rule about custom directives: on a component they apply to the root element only, and are ignored with a warning on multi-root components.

Gotcha· Composables are called during setup, not only in script setup

"Use only in script setup" is close. The rule is to call them synchronously during setup, in <script setup> or setup(), because their lifecycle hooks and watchers attach to the component whose setup is running.

Exercise

Exercise
  • Write usePagedList(page) from the questions and call it with a number, a ref and a getter.
  • Register v-color globally and change the bound value to see the function shorthand handle updates.
  • Turn the i18n plugin from the questions into a real one with two languages and a locale ref.

Be able to say

Be able to say· What makes a good composable?

"It is a use-prefixed function that encapsulates stateful logic, is called synchronously during setup, and returns a plain object of refs so callers can destructure it. It accepts refs, getters or plain values and reads them with toValue() inside a watchEffect or computed, so it reacts when they change. It cleans up after itself, because its lifecycle hooks attach to the calling component. State created outside the function is shared by every caller, which on a server means every request, so in Nuxt that kind of state goes in useState."

Be able to say· When would you write a custom directive or a plugin?

"A directive only for low-level DOM work on an element, like focusing an input when it appears or wiring a DOM library, and not on components. A plugin when something has to be set up once for the whole app: global components or directives, app.provide for a service, an error handler, analytics. Libraries like Vue Router and i18n are plugins that expose composables through provide/inject."

Your notes

Open in Notes

Nothing yet. Notes filed here show up on this page and the section overview.