-
Notifications
You must be signed in to change notification settings - Fork 4
/
page.haml
77 lines (55 loc) · 1.86 KB
/
page.haml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
:ruby
Card = import("/app/components/UI/Card")
Highlight = import("/app/components/UI/Highlight")
Markdown = import("../Markdown")
Example = import("./Example")
%article
%Markdown
:plain
# Lifecycle methods
There are a few lifecycle methods that can be useful.
%Highlight(language="haml")
= File.read("app/pages/docs/lifecycle-methods/Example.haml")
%Card
%Example
%Markdown
:plain
## List of lifecycle methods
**`def self.get_initial_state(**props)`**
Called when initializing the component.
It has to return a Hash with the initial state.
**`def mount`**
Called when the component gets mounted.
This is a good place to start async task or periodic updates.
All tasks will be stopped when the component unmounts.
```haml
:ruby
def mount
loop do
update(time: Time.now.to_s)
sleep 1
end
end
%p= state[:time]
```
This component will update the current time every second.
**`def unmount`**
Called after the component has been unmounted.
**`def should_update?(next_props, next_state)`**
By default, components will re-render on every change,
but in some cases you might want to skip rendering to
improve performance.
Usually you don't need to override the default behavior.
Should return `true` or `false`.
**`def did_update(prev_props, prev_state)`**
The `did_update` method is called right
after a component has been updated.
**`def self.get_derived_state_from_props(props, state)`**
Should return `nil` or new state.
[React documentation: getDerivedStateFromProps](https://reactjs.org/docs/react-component.html#static-getderivedstatefromprops)
:css
Card {
margin: 1em;
margin-top: 2em;
padding: 1em;
}