0Pricing
Sveltejs Academy · Lesson

Custom Store Pattern with subscribe/set/update

Build a store with encapsulated logic by exposing a subscribe method and custom actions.

Store Contract

Any object with a subscribe(fn) method that returns an unsubscribe function is a valid Svelte store.

Wrap a writable

Build a custom store by wrapping a writable and exposing a focused API.

function createCounter() {
  const { subscribe, set, update } = writable(0);
  return {
    subscribe,
    increment: () => update(n => n + 1),
    reset: () => set(0)
  };
}

All lessons in this course

  1. derived() for Computed Stores
  2. Custom Store Pattern with subscribe/set/update
  3. Readable from Writable
  4. Store Contracts and Interoperability
← Back to Sveltejs Academy