createEvent
import { createEvent } from "effector";
const event = createEvent();Method for creating events.
Formula
createEvent<E = void>(eventName?: string): EventCallable<E>createEvent<E = void>(config: { name?: string sid?: string domain?: Domain}): EventCallable<E>-
Arguments
-
eventName: Optional argument. Event name for debugging. -
config: Optional argument. Configuration object.name: Event name.sid: Stable identifier for SSR.domain: Domain for the event.
-
-
Return value
Returns a new callable event.
Examples
Updating state by calling an event:
import { createStore, createEvent } from "effector";
const addNumber = createEvent();
const $counter = createStore(0);
$counter.on(addNumber, (state, number) => state + number);
$counter.watch((state) => { console.log("state", state);});// => 0
addNumber(10);// => 10
addNumber(10);// => 20
addNumber(10);// => 30We created the addNumber event and the $counter store, then subscribed to store updates.
Notice the function call addNumber(10). Every time you call addNumber(10), you can check the console and see how the state changes.
Processing data with derived events:
import { createEvent } from "effector";
const extractPartOfArray = createEvent();const array = extractPartOfArray.map((arr) => arr.slice(2));
array.watch((part) => { console.log(part);});extractPartOfArray([1, 2, 3, 4, 5, 6]);// => [3, 4, 5, 6]Common errors
Below is a list of possible errors you may encounter when working with events:
call of derived event is not supported, use createEvent insteadunit call from pure function is not supported, use operators like sample instead
Related API and Articles
- API
- Articles