Skip to content
On this page

Store Events

Each store has a few events, which you listen:

  • onMount(store, cb): first listener was subscribed with debounce. We recommend to always use onMount instead of onStart + onStop, because it has a short delay to prevent flickering behavior.
  • onStart(store, cb): first listener was subscribed. Low-level method. It is better to use onMount for simple lazy stores.
  • onStop(store, cb): last listener was unsubscribed. Low-level method. It is better to use onMount for simple lazy stores.
  • onSet(store, cb): before applying any changes to the store.
  • onNotify(store, cb): before notifying store’s listeners about changes.

onSet and onNotify events has abort() function to prevent changes or notification.

import { onSet } from 'nanostores'
onSet($store, ({ newValue, abort }) => {
if (!validate(newValue)) {
abort()
}
})

Event listeners can communicate with payload.shared object.


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