kea-subscriptions

Value change subscriptions for Kea

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
kea-subscriptions
003.0.05 months ago5 months agoMinified + gzip package size for kea-subscriptions in KB

Readme

NPM Version minified minified + gzipped Backers on Open Collective Sponsors on Open Collective
kea-subscriptions
Subscribe to changes in values. Works with kea 3.0.0 and up.

Sample usage

Read the documentation
import { kea, actions, reducers } from 'kea'
import { subscriptionsPlugin, subscriptions } from 'kea-subscriptions'

// once per app
resetContext({ plugins: [subscriptionsPlugin] }) // hook into redux

const logic = kea([
  actions({ setMyValue: (value) => ({ value }) }),
  reducers({ myValue: ['default', { setMyValue: (_, { value }) => value }] }),
  subscriptions({ myValue: (value, oldValue) => console.log({ value, oldValue }) }),
])

logic.mount()
// [console.log] { value: 'default', oldValue: undefined }
logic.actions.setMyValue('coffee')
// [console.log] { value: 'coffee', oldValue: 'default' }
logic.actions.setMyValue('bagels')
// [console.log] { value: 'bagels', oldValue: 'coffee' }