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

Commit da5927f

Browse files
committed
fix bug in ref callback, with spec
1 parent 3117fe7 commit da5927f

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

lib/react/api.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ def self.convert_props(properties)
174174
elsif key == 'ref' && value.is_a?(Proc)
175175
props[key] = %x{
176176
function(dom_node){
177-
if (dom_node.__opalInstance !== undefined && dom_node.__opalInstance !== null) {
177+
if (dom_node !== null && dom_node.__opalInstance !== undefined && dom_node.__opalInstance !== null) {
178178
#{ value.call(`dom_node.__opalInstance`) };
179-
} else if(ReactDOM.findDOMNode !== undefined && dom_node.nodeType === undefined) {
179+
} else if(dom_node !== null && ReactDOM.findDOMNode !== undefined && dom_node.nodeType === undefined) {
180180
#{ value.call(`ReactDOM.findDOMNode(dom_node)`) };
181181
} else {
182182
#{ value.call(`dom_node`) };

lib/react/ref_callback.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ def self.convert_props(properties)
1414
props.map do |key, value|
1515
if key == "ref" && value.is_a?(Proc)
1616
new_proc = Proc.new do |native_inst|
17-
if `#{native_inst}.__opalInstance !== undefined && #{native_inst}.__opalInstance !== null`
17+
if `#{native_inst} !== null && #{native_inst}.__opalInstance !== undefined && #{native_inst}.__opalInstance !== null`
1818
value.call(`#{native_inst}.__opalInstance`)
19-
elsif `ReactDOM.findDOMNode !== undefined && #{native_inst}.nodeType === undefined`
19+
elsif `#{native_inst} !== null && ReactDOM.findDOMNode !== undefined && #{native_inst}.nodeType === undefined`
2020
value.call(`ReactDOM.findDOMNode(#{native_inst})`) # react >= v0.15.`)
2121
else
2222
value.call(native_inst)

spec/react/refs_callback_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,33 @@ def render
6262
"#{Foo.bar.JS['nodeType']}" # aboids json serialisation errors by using "#{}"
6363
end.to eq("1")
6464
end
65+
66+
it "works, even when the component is unmounted" do
67+
# was a bug, on umount react calls the ref method with null instead of a dom node
68+
# callback failed then
69+
# ref is called two times, once on mount with dom_node, once on unmount with null
70+
mount "Foo" do
71+
class Unmountable < Hyperloop::Component
72+
render do
73+
DIV { "This is a Component" }
74+
end
75+
end
76+
Foo.class_eval do
77+
def ref_rec(dom_node)
78+
@@rec_cnt ||= 0
79+
@@rec_cnt += 1
80+
end
81+
def self.rec_cnt
82+
@@rec_cnt
83+
end
84+
85+
after_mount { mutate.unmount true }
86+
87+
render do
88+
Unmountable(ref: method(:ref_rec).to_proc) unless state.unmount
89+
end
90+
end
91+
end
92+
expect_evaluate_ruby('Foo.rec_cnt').to eq(2)
93+
end
6594
end

0 commit comments

Comments
 (0)