Skip to content

State management

This guide describes where frontend state in app/frontend/src/ lives, how stores expose and update it, and how components read it without losing reactivity. The examples use a made-up bookmarks module: they show the pattern, not existing code. For parent-child ownership through props and emits, see Components.

Local vs global state

Choose the narrowest scope that serves every reader of the state:

  • Local state: ref, computed or a composable inside one component. Use it for form inputs, toggles and dialog visibility that no other component reads.
  • Feature state: a Pinia store shared by the components of one feature. The store lives in the Pinia of the Vue app that hosts the feature; a feature split into several apps shares one Pinia from its root (see below).
  • Application-wide state: a deliberate singleton for a concern that spans apps or legacy JavaScript, such as tracking which dialogs are open.

A store is not the default. A component that only displays data or reports an interaction receives props and emits events, even when its parent reads a store.

Global state patterns (stores)

1. Pinia store (Vue app scoped)

Use a Pinia store when several components of one feature share state. Keep updates in the store's actions and expose state that only those actions may change with readonly():

export const useBookmarksStore = defineStore("bookmarksStore", () => {
  const bookmarks = ref<Bookmark[]>([]);
  const draft = ref<BookmarkDraft>({ ...EMPTY_BOOKMARK_DRAFT });
  const saving = ref(false);

  const setBookmarks = (items: Bookmark[]) => {
    bookmarks.value = items;
  };

  const setSaving = (value: boolean) => {
    saving.value = value;
  };

  return {
    bookmarks: readonly(bookmarks),
    draft,
    saving: readonly(saving),
    setBookmarks,
    setSaving,
  };
});

When the store's contract is "change through actions", call the action instead of assigning to its state from a component.

Feature-root Pinia shared across islands

A feature mounted as several Vue apps (islands) can still share one store. Its root creates one Pinia per feature instance, installs it in every island and disposes it when the instance is released:

const pinia = createPinia();

const apps = islands.map(({ component, target }) => {
  const app = createApp(component);
  app.use(pinia);
  app.mount(target);
  return app;
});

const release = () => {
  apps.forEach(app => app.unmount());
  disposePinia(pinia);
};

The islands then read the same store state, which the root loads once before mounting, for example with useBookmarksStore(pinia).setBookmarks(initialBookmarks). Two instances of the feature on one page get separate Pinia instances, so their stores do not leak into each other.

2. Singleton store (framework-agnostic)

Use a singleton only for a concern that is application-wide by nature, must be reachable from several Vue apps or from legacy JavaScript, and has no feature root to own it:

class OpenDialogsStore {
  private static instance: OpenDialogsStore = new OpenDialogsStore();
  private openDialogIds = new Set<string>();

  static getInstance(): OpenDialogsStore {
    return OpenDialogsStore.instance;
  }

  public get hasOpenDialog(): boolean {
    return this.openDialogIds.size > 0;
  }

  public setOpened(dialogId: string, opened: boolean) {
    if (opened) {
      this.openDialogIds.add(dialogId);
    } else {
      this.openDialogIds.delete(dialogId);
    }
  }
}

export const openDialogsStore = OpenDialogsStore.getInstance();

Every caller imports the same instance:

openDialogsStore.setOpened(dialogId, true);

Do not add a singleton for state that one feature owns; use that feature's Pinia store.

Reading store state

Prefer a named store constant and read state as store.foo. It stays reactive and shows where the value comes from:

<DSActionButtons :disabled="bookmarksStore.saving" />

<script setup lang="ts">
const bookmarksStore = useBookmarksStore();
</script>

Use storeToRefs(store) when a composable returns store state or passes it on as refs. Actions are exposed directly:

const store = useBookmarksStore();
const { bookmarks, draft } = storeToRefs(store);

return { bookmarks, draft, setBookmarks: store.setBookmarks };

Do not destructure state from the store itself. const { saving } = useBookmarksStore() copies the current value and stops tracking changes. Actions are plain functions and can be destructured or passed on.

Best practices

  • Keep state local until another component genuinely needs it.
  • Use a feature Pinia store for state shared within one feature, including across its islands.
  • Use a singleton only for deliberately application-wide concerns reachable from outside one Vue app.
  • Read through a named store constant; use storeToRefs only to return or pass reactive refs.
  • Update through actions where that is the store's contract, and expose such state with readonly().
  • Keep stores modular: one concern per store.