Skip to main content

createEvent

Method for creating an event

createEvent<T>(name?): Event<T>
createEvent(name?): Event<void>

Arguments

  1. name? (string): Event name

Returns

Event: New event

Notes#

Event - it is a function which allows to change state when called (see example 1) also it can be a good way to extract data (see example 2)

Example 1#

import {createStore, createEvent} from 'effector'
const addNumber = createEvent()
const $store = createStore(0).on(addNumber, (state, number) => state + number)
$store.watch(state => {
console.log('state', state)
})
// => 0
addNumber(10)
// => 10
addNumber(10)
// => 20
addNumber(10)
// => 30

Try it

We created a store and an event (addNumber), and started watching the store.
Notice the function call addNumber(10). Whenever you will call addNumber(10), you can look at the console and see how state changes.

Example 2#

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]

Try it

Last updated on