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

Commit 5933b81

Browse files
committed
Fix #170
1 parent 4e5d1fb commit 5933b81

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Whitespace conventions:
2626

2727
### Fixed
2828

29+
- `Element#render` trigger unnecessary re-mounts when called multiple times. (#170)
2930
- Gets rid of react warnings about updating state during render (#155)
3031
- Multiple HAML classes (i.e. div.foo.bar) was not working (regression introduced in 0.8.8)
3132
- Don't send nil (null) to form components as the value string (#157)

lib/react/top_level.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,14 @@ def self.[](selector)
119119
end
120120

121121
define_method :render do |container = nil, params = {}, &block|
122-
klass = Class.new(React::Component::Base)
122+
if `#{self.to_n}._reactrb_component_class === undefined`
123+
`#{self.to_n}._reactrb_component_class = #{Class.new(React::Component::Base)}`
124+
end
125+
klass = `#{self.to_n}._reactrb_component_class`
123126
klass.class_eval do
124127
render(container, params, &block)
125128
end
126-
React.render(React.create_element(klass), self)
129+
130+
React.render(React.create_element(`#{self.to_n}._reactrb_component_class`), self)
127131
end
128132
end if Object.const_defined?('Element')

spec/react/opal_jquery_extensions_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,27 @@
66
React::API.clear_component_class_cache
77
end
88

9+
it 'will reuse the wrapper componet class for the same Element' do
10+
stub_const 'Foo', Class.new(React::Component::Base)
11+
Foo.class_eval do
12+
param :name
13+
def render
14+
"hello #{params.name}"
15+
end
16+
17+
def component_will_unmount
18+
19+
end
20+
end
21+
22+
expect_any_instance_of(Foo).to_not receive(:component_will_unmount)
23+
24+
test_div = Element.new(:div)
25+
test_div.render { Foo(name: 'fred') }
26+
test_div.render { Foo(name: 'freddy') }
27+
expect(Element[test_div].find('span').html).to eq('hello freddy')
28+
end
29+
930
it 'renders a top level component using render with a block' do
1031
stub_const 'Foo', Class.new(React::Component::Base)
1132
Foo.class_eval do

0 commit comments

Comments
 (0)