import { createWatch } from "effector";

Methods

createWatch(config)

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

Formulae

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 the unit is triggered. Accepts the unit’s payload as the first argument.
    • scope (Scope): An optional scope object (forked instance) to restrict watcher calls on particular scope.

Returns

Subscription: Unsubscribe function

Examples

With 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

Without 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
Contributors