UNPKG

1.31 kBMarkdownView Raw
1## Events
2
3Events are part of `on` method in every Component. It can also be defined inside `constructor` as simple object. Every method that is part of it is event handler. Every event can also be an [action](#actions).
4
5```js
6this.on = {
7 buyMilk() {
8 if (this.state.milk === 0) {
9 console.log('Milk not found')
10 } else {
11 console.log('Here you go')
12 }
13 },
14
15 @action
16 outOfMilk() {
17 return {
18 milk: 0
19 }
20 }
21}
22```
23
24```js
25import { action, Component } from 'radi';
26
27class Grandma extends Component {
28 state() {
29 return {
30 status: 'busy'
31 }
32 }
33
34 on() {
35 return {
36 callGrandma(whatToSay) {
37 console.log('Grandma is ', this.state.status, 'try to say', whatToSay, 'again later')
38 }
39 }
40 }
41
42 call() {
43 this.trigger('callGrandma', 'hello')
44 }
45}
46```
47
48### Component lifecycle
49
50Radi supports lifecycle events for Components. Currently two events are defined: `mount` and `destroy`.
51
52- When Component gets mounted, `mount` gets called.
53- If Component gets unmounted and is no longer part of DOM, `destroy` gets called.
54
55```js
56this.on = {
57 mount() {
58 console.log('I just got mounted')
59 },
60 destroy() {
61 console.log('Components view was destroyed, but I can still be mounted again')
62 }
63}
64```
65
66### Global event handling
67
68Coming Soon