Skip to main content

createWatch

Creates a subscription on unit (store, event or effect).

createWatch<T>(config: {
unit: Unit<T>
fn: (payload: T) => void
scope?: Scope
}): Subscription

Arguments

  1. config (Object): Configuration
    • unit (Unit): Target unit (store, event of effect) that will be watched
    • fn (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