kea-subscriptions
v3.0.1
Published
Value change subscriptions for Kea
Downloads
25,710
Readme
kea-subscriptions
Subscribe to changes in values. Works with kea 3.0.0
and up.
Sample usage
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' }