Эта страница еще не переведена

Чтобы добавить перевод, откройте Pull Request по этой ссылке.

Отображается содержимое для языка по умолчанию.

import { type Domain } from "effector";

Domain is a namespace for your events, stores and effects.

Domain can subscribe to event, effect, store or nested domain creation with onCreateEvent, onCreateStore, onCreateEffect, onCreateDomain methods.

It is useful for logging or other side effects.

Unit creators

createEvent(name?)

Arguments

  1. name? (string): event name

Returns

Event: New event

createEffect(handler?)

Creates an effect with given handler.

Arguments

  1. handler? (Function): function to handle effect calls, also can be set with use(handler)

Returns

Effect: A container for async function.

createEffect(name?)

Arguments

  1. name? (string): effect name

Returns

Effect: A container for async function.

createStore(defaultState)

Arguments

  1. defaultState (State): store default state

Returns

Store: New store

createDomain(name?)

Arguments

  1. name? (string): domain name

Returns

Domain: New domain

Aliases

event(name?)

An alias for domain.createEvent

effect(name?)

An alias for domain.createEffect

store(defaultState)

An alias for domain.createStore

domain(name?)

An alias for domain.createDomain

Domain Properties

.history

Contains mutable read-only sets of units inside a domain.

Formulae

interface DomainHistory {
  stores: Set<Store<any>>;
  events: Set<Event<any>>;
  domains: Set<Domain>;
  effects: Set<Effect<any, any, any>>;
}

const { stores, events, domains, effects } = domain.history;

When any kind of unit created inside a domain, it appears in a set with the name of type(stores, events, domains, effects) in the same order as created.

Examples

Basic

import { createDomain } from "effector";
const domain = createDomain();
const eventA = domain.event();
const $storeB = domain.store(0);
console.log(domain.history);
// => {stores: Set{storeB}, events: Set{eventA}, domains: Set, effects: Set}

Try it

Domain hooks

onCreateEvent(callback)

Formulae

domain.onCreateEvent((event: Event<any>) => {});
  • Function passed to onCreateEvent called every time, as new event created in domain
  • Function called with event as first argument
  • The result of function call is ignored

Arguments

  1. callback (Watcher): A function that receives Event and will be called during every domain.createEvent call

Returns

Subscription: Unsubscribe function.

Example

import { createDomain } from "effector";

const domain = createDomain();

domain.onCreateEvent((event) => {
  console.log("new event created");
});

const a = domain.createEvent();
// => new event created

const b = domain.createEvent();
// => new event created

Try it

onCreateEffect(callback)

Formulae

domain.onCreateEffect((effect: Effect<any, any, any>) => {});
  • Function passed to onCreateEffect called every time, as new effect created in domain
  • Function called with effect as first argument
  • The result of function call is ignored

Arguments

  1. callback (Watcher): A function that receives Effect and will be called during every domain.createEffect call

Returns

Subscription: Unsubscribe function.

Example

import { createDomain } from "effector";

const domain = createDomain();

domain.onCreateEffect((effect) => {
  console.log("new effect created");
});

const fooFx = domain.createEffect();
// => new effect created

const barFx = domain.createEffect();
// => new effect created

Try it

onCreateStore(callback)

Formulae

domain.onCreateStore(($store: Store<any>) => {});
  • Function passed to onCreateStore called every time, as new store created in domain
  • Function called with $store as first argument
  • The result of function call is ignored

Arguments

  1. callback (Watcher): A function that receives Store and will be called during every domain.createStore call

Returns

Subscription: Unsubscribe function.

Example

import { createDomain } from "effector";

const domain = createDomain();

domain.onCreateStore((store) => {
  console.log("new store created");
});

const $a = domain.createStore(null);
// => new store created

Try it

onCreateDomain(callback)

Formulae

domain.onCreateDomain((domain) => {});
  • Function passed to onCreateDomain called every time, as subdomain created in domain
  • Function called with domain as first argument
  • The result of function call is ignored

Arguments

  1. callback (Watcher): A function that receives Domain and will be called during every domain.createDomain call

Returns

Subscription: Unsubscribe function.

Example

import { createDomain } from "effector";

const domain = createDomain();

domain.onCreateDomain((domain) => {
  console.log("new domain created");
});

const a = domain.createDomain();
// => new domain created

const b = domain.createDomain();
// => new domain created

Try it

Перевод поддерживается сообществом

Документация на английском языке - самая актуальная, поскольку её пишет и обновляет команда effector. Перевод документации на другие языки осуществляется сообществом по мере наличия сил и желания.

Помните, что переведенные статьи могут быть неактуальными, поэтому для получения наиболее точной и актуальной информации рекомендуем использовать оригинальную англоязычную версию документации.

Соавторы