0Pricing
Sveltejs Academy · Lesson

Readable from Writable

Expose only the read side of a writable store to consumers.

Why Expose Read-Only

You may want internal writes but read-only access for consumers, preventing accidental mutation.

Pattern

Keep the writable private; export an object with only the subscribe method.

const _user = writable(null);
export const user = { subscribe: _user.subscribe };
export function setUser(u) { _user.set(u); }

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