Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

Commit 27ddbc6

Browse files
committed
Refactor using React::Callbacks
1 parent f7e867f commit 27ddbc6

File tree

1 file changed

+7
-30
lines changed

1 file changed

+7
-30
lines changed

lib/react/component.rb

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
require "./ext/string"
22
require 'active_support/core_ext/class/attribute'
3+
require 'react/callbacks'
34

45
module React
56
module Component
67
def self.included(base)
8+
base.include(React::Callbacks)
79
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
913
end
1014
base.extend(ClassMethods)
11-
12-
base.before_mount_callbacks = []
13-
base.after_mount_callbacks = []
1415
end
1516

1617
def initialize(native_element)
@@ -34,25 +35,11 @@ def mounted?
3435
end
3536

3637
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)
4539
end
4640

4741
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)
5643
end
5744

5845
def component_will_receive_props(next_props)
@@ -129,16 +116,6 @@ def params(&block)
129116
self.validator = React::Validator.build(&block)
130117
end
131118

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-
142119
def define_state(*states)
143120
raise "Block could be only given when define exactly one state" if block_given? && states.count > 1
144121

0 commit comments

Comments
 (0)