Skip to content
On this page

Effects

effect subscribes for multiple atoms at once.

effect runs its callback on the start, with initial values, as well as on any stores change. If callback returns cleanup function it will be performed before next callback run. Besides that, effect returns own cleanup function, which allows cancelling the whole effect.

const $enabled = atom(true)
const $interval = atom(1000)
const cancelPing = effect([$enabled, $interval], (enabled, interval) => {
if (!enabled) return
const intervalId = setInterval(() => {
sendPing()
}, interval)
return () => {
clearInterval(intervalId)
}
})

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