1
1
require "./ext/string"
2
2
require 'active_support/core_ext/class/attribute'
3
+ require 'react/callbacks'
3
4
4
5
module React
5
6
module Component
6
7
def self . included ( base )
8
+ base . include ( React ::Callbacks )
7
9
base . class_eval do
8
- class_attribute :before_mount_callbacks , :after_mount_callbacks , :init_state , :validator
10
+ class_attribute :init_state , :validator
11
+ define_callback :before_mount
12
+ define_callback :after_mount
9
13
end
10
14
base . extend ( ClassMethods )
11
-
12
- base . before_mount_callbacks = [ ]
13
- base . after_mount_callbacks = [ ]
14
15
end
15
16
16
17
def initialize ( native_element )
@@ -34,25 +35,11 @@ def mounted?
34
35
end
35
36
36
37
def component_will_mount
37
- return unless self . class . before_mount_callbacks
38
- self . class . before_mount_callbacks . each do |callback |
39
- if callback . is_a? ( Proc )
40
- callback . call
41
- else
42
- send ( callback )
43
- end
44
- end
38
+ self . run_callback ( :before_mount )
45
39
end
46
40
47
41
def component_did_mount
48
- return unless self . class . after_mount_callbacks
49
- self . class . after_mount_callbacks . each do |callback |
50
- if callback . is_a? ( Proc )
51
- callback . call
52
- else
53
- send ( callback )
54
- end
55
- end
42
+ self . run_callback ( :after_mount )
56
43
end
57
44
58
45
def component_will_receive_props ( next_props )
@@ -129,16 +116,6 @@ def params(&block)
129
116
self . validator = React ::Validator . build ( &block )
130
117
end
131
118
132
- def before_mount ( *callback , &block )
133
- self . before_mount_callbacks . concat callback
134
- self . before_mount_callbacks << block if block_given?
135
- end
136
-
137
- def after_mount ( *callback , &block )
138
- self . after_mount_callbacks . concat callback
139
- self . after_mount_callbacks << block if block_given?
140
- end
141
-
142
119
def define_state ( *states )
143
120
raise "Block could be only given when define exactly one state" if block_given? && states . count > 1
144
121
0 commit comments