createWatch
Creates a subscription on unit (store, event or effect).
createWatch<T>(config: {
unit: Unit<T>
fn: (payload: T) => void
scope?: Scope
}): Subscription
Arguments
config
(Object): Configurationunit
(Unit): Target unit (store, event of effect) that will be watchedfn
(Function): Function that will be called when unit is triggered. Accepts unit payload as the first argument.scope
(Scope): An optional scope object (forked instance) to restrict watcher calls on particular scope.
Returns
Subscription: Unsubscribe function
Example (scope)
import {createWatch, createEvent, fork, allSettled} from 'effector'
const changeName = createEvent()
const scope = fork()
const unwatch = createWatch({unit: changeName, scope, fn: console.log})
await allSettled(changeName, {scope, params: 'John'}) // output: John
changeName('John') // no output
Example (no scope)
import {createWatch, createEvent, fork, allSettled} from 'effector'
const changeName = createEvent()
const scope = fork()
const unwatch = createWatch({unit: changeName, fn: console.log})
await allSettled(changeName, {scope, params: 'John'}) // output: John
changeName('John') // output: John