Skip to content
On this page

Vanilla JS

Store#subscribe() calls callback immediately and subscribes to store changes. It passes store’s value to callback.

import { $profile } from '../stores/profile.js'
$profile.subscribe(profile => {
console.log(`Hi, ${profile.name}`)
})

Store#listen(cb) in contrast, calls only on next store change. It could be useful for a multiple stores listeners.

function render() {
console.log(`${$post.get().title} for ${$profile.get().name}`)
}
$profile.listen(render)
$post.listen(render)
render()

See also listenKeys(store, keys, cb) to listen for specific keys changes in the map.


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