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
): An intention to change state (see notes).
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
const store = createStore(0)
const addNumber = createEvent()
store.on(addNumber, (state, number) => state + number)
store.watch(state => console.log(state)) // 10 20 30
addNumber(10)
addNumber(10)
addNumber(10)
Let's talk about what happened. We created store and event (addNumber), and started to watch after 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
const extractPartOfArray = createEvent()
const array = extractPartOfArray.map(arr => arr.slice(2))
array.watch(console.log) // [3,4,5,6]
extractPartOfArray([1, 2, 3, 4, 5, 6])