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

Commit f0eb5f7

Browse files
committed
Implement setProps & replaceProps
1 parent dbfedee commit f0eb5f7

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

lib/react/component.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,24 @@ module API
156156
alias_native :mounted?, :isMounted
157157
alias_native :force_update!, :forceUpdate
158158

159+
def set_props(prop, &block)
160+
raise "No native ReactComponent associated" unless @native
161+
%x{
162+
#{@native}.setProps(#{prop.to_n}, function(){
163+
#{block.call if block}
164+
});
165+
}
166+
end
167+
168+
def set_props!(prop, &block)
169+
raise "No native ReactComponent associated" unless @native
170+
%x{
171+
#{@native}.replaceProps(#{prop.to_n}, function(){
172+
#{block.call if block}
173+
});
174+
}
175+
end
176+
159177
def set_state(state, &block)
160178
raise "No native ReactComponent associated" unless @native
161179
%x{

spec/component_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,39 @@ def render
280280
end
281281
end
282282

283+
describe "Props Updating" do
284+
before do
285+
stub_const 'Foo', Class.new
286+
Foo.class_eval do
287+
include React::Component
288+
end
289+
end
290+
291+
it "should support original `setProps` as method `set_props`" do
292+
Foo.class_eval do
293+
def render
294+
React.create_element("div") { params[:foo] }
295+
end
296+
end
297+
298+
element = renderToDocument(Foo, {foo: 10})
299+
element.set_props(foo: 20)
300+
expect(element.dom_node.innerHTML).to eq('20')
301+
end
302+
303+
it "should support original `replaceProps` as method `set_props!`" do
304+
Foo.class_eval do
305+
def render
306+
React.create_element("div") { params[:foo] ? "exist" : "null" }
307+
end
308+
end
309+
310+
element = renderToDocument(Foo, {foo: 10})
311+
element.set_props!(bar: 20)
312+
expect(element.dom_node.innerHTML).to eq('null')
313+
end
314+
end
315+
283316
describe "Prop validation" do
284317
before do
285318
stub_const 'Foo', Class.new

0 commit comments

Comments
 (0)