createEvent(name?)
Creates an Event. In other words you create intention.
You can pass name
if you want.
Arguments
name
? (string): Event name.
Returns
Event: New event
Notes
Event - it is a function which called allow to change state, see Example 1 also it could be good opportunity to data extract, see Example 2 and we discuss it next section.
Example 1
import {createStore, createEvent} from 'effector'const store = createStore(0)const addNumber = createEvent()store.on(addNumber, (state, number) => state + number)store.watch(state => { console.log('state', state)})// => 0
addNumber(10)// => 10
addNumber(10)// => 20
addNumber(10)// => 30
Let's talk about what happened. We created store and event (addNumber), and started to watch the store.
You should pay attention to addNumber(10)
. Whenever you will call addNumber(10)
you may see in console how state will change.
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]