Skip to content

Commit a01f333

Browse files
committed
proper detection of stateless native components
1 parent 97c018a commit a01f333

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

ruby/hyper-component/lib/hyperstack/component/element.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def initialize(native_element, type = nil, properties = {}, block = nil)
3434
end
3535

3636
def _update_ref(x)
37+
puts "#{self}._update_ref(#{x})"
3738
@ref = x
3839
@_child_element._update_ref(x) if @_child_element
3940
end

ruby/hyper-component/lib/hyperstack/internal/component/react_wrapper.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ module Component
1818
class ReactWrapper
1919
@@component_classes = {}
2020

21+
def self.stateless?(ncc)
22+
%x{
23+
typeof #{ncc} === 'function' // can be various things
24+
&& !(
25+
#{ncc}.prototype // native arrows don't have prototypes
26+
&& #{ncc}.prototype.isReactComponent // special property
27+
)
28+
}
29+
end
30+
2131
def self.import_native_component(opal_class, native_class)
2232
opal_class.instance_variable_set("@native_import", true)
2333
@@component_classes[opal_class] = native_class
@@ -29,9 +39,8 @@ def self.eval_native_react_component(name)
2939
is_component_class = `#{component}.prototype !== undefined` &&
3040
(`!!#{component}.prototype.isReactComponent` ||
3141
`!!#{component}.prototype.render`)
32-
is_functional_component = `typeof #{component} === "function"`
3342
has_render_method = `typeof #{component}.render === "function"`
34-
unless is_component_class || is_functional_component || has_render_method
43+
unless is_component_class || stateless?(component) || has_render_method
3544
raise 'does not appear to be a native react component'
3645
end
3746
component
@@ -179,7 +188,7 @@ def self.create_element(type, *args, &block)
179188

180189
# Convert Passed in properties
181190
ele = nil # create nil var for the ref to use
182-
ref = ->(ref) { ele._update_ref(ref) } unless `typeof ncc === 'function'`
191+
ref = ->(ref) { ele._update_ref(ref) } unless stateless?(ncc)
183192
properties = convert_props(type, { ref: ref }, *args)
184193
params << properties.shallow_to_n
185194

@@ -192,7 +201,9 @@ def self.create_element(type, *args, &block)
192201
}
193202
}
194203
end
195-
ele = Hyperstack::Component::Element.new(`React.createElement.apply(null, #{params})`, type, properties, block)
204+
Hyperstack::Component::Element.new(
205+
`React.createElement.apply(null, #{params})`, type, properties, block
206+
)
196207
end
197208

198209
def self.clear_component_class_cache

0 commit comments

Comments
 (0)