Skip to content
On this page

Batching

batch groups multiple store writes into a single transaction. Listeners and effects fire at most once when the outermost batch returns, observing the final values. Batches can be nested; only the outermost flush triggers the notifications.

import { atom, batch } from 'nanostores'
const $firstName = atom('')
const $lastName = atom('')
batch(() => {
$firstName.set('Ada')
$lastName.set('Lovelace')
})
// effects depending on either atom run once, with both new values visible

Inside a batch, Map#setKey writes are coalesced too. The listener fires once and its changed argument is undefined (the batch touched multiple keys), so listenKeys subscribers fire once regardless of which key they watch. Outside a batch, each setKey still notifies with its own changed key as before.


Imported from the Nano Stores README for version 1.5.5. Improve this page there.