0Pricing
Sveltejs Academy · Lesson

Updating Stores: set() and update()

Replace or transform store values using set() and the updater callback in update().

Updating Stores: set() and update() is a free Sveltejs Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Sveltejs Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Two Update Modes

Stores expose two update methods: set(value) to replace and update(fn) to transform.

set Example

Replace the whole value.

count.set(5);

update Example

Transform the current value using a callback.

count.update(n => n + 1);

When to Use Which

Use set for absolute changes; use update when the new value depends on the old.

Inside Components

Inside .svelte files, prefer the $store = x shorthand to store.set(x).

Outside Components

In plain JS modules and helpers, call set/update directly.

Object Updates

When the store holds an object, return a new object from update to ensure subscribers see a change.

user.update(u => ({ ...u, name }));

Array Updates

Same rule: return a new array, do not mutate in place.

todos.update(list => [...list, newTodo]);

Batching

Multiple sequential updates each fire subscribers. For batches, compute the final value and set once.

Async Updates

You can call set inside async callbacks; subscribers receive the new value when it fires.

Custom Setters

Wrap set/update in named functions on a custom store for cleaner APIs.

function increment() { count.update(n => n + 1); }

Quick Check

Which method needs the previous value?

Recap

set replaces; update transforms using the current value via a callback.

Frequently asked questions

Is the “Updating Stores: set() and update()” lesson free?

Yes — the full text of “Updating Stores: set() and update()” is free to read here on the web, and the Sveltejs Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Sveltejs Academy course, upgrade to CoddyKit PRO.

What will I learn in “Updating Stores: set() and update()”?

Replace or transform store values using set() and the updater callback in update(). You practise Sveltejs Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Sveltejs Academy?

No prior experience is required. Sveltejs Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Updating Stores: set() and update()” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Sveltejs Academy lesson?

Yes. Every Sveltejs Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Creating a writable Store
  2. Subscribing with $ Auto-subscription
  3. Updating Stores: set() and update()
  4. readable Stores and Custom Start Functions
← Back to Sveltejs Academy