Skip to content

Commit 41600e8

Browse files
authored
Merge pull request #642 from NGMarmaduke/camelize-render-option
Allow `camelize_props` to be passed into component mounter
2 parents 3273820 + ca11362 commit 41600e8

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@ MyApp::Application.configure do
203203
end
204204
```
205205

206+
or when mounting:
207+
208+
```erb
209+
<%= react_component('HelloMessage', {name: 'John'}, {camelize_props: true}) %>
210+
```
211+
206212
### Rendering components instead of views
207213

208214
Components can also be prerendered directly from a controller action with the custom `component` renderer. For example:

lib/react/rails/component_mount.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def teardown(env)
2424
# on the client.
2525
def react_component(name, props = {}, options = {}, &block)
2626
options = {:tag => options} if options.is_a?(Symbol)
27-
if camelize_props_switch
27+
if camelize_props_switch || options[:camelize_props]
2828
props = React.camelize_props(props)
2929
end
3030

@@ -43,7 +43,7 @@ def react_component(name, props = {}, options = {}, &block)
4343
html_tag = html_options[:tag] || :div
4444

4545
# remove internally used properties so they aren't rendered to DOM
46-
html_options.except!(:tag, :prerender)
46+
html_options.except!(:tag, :prerender, :camelize_props)
4747

4848
content_tag(html_tag, '', html_options, &block)
4949
end

test/react/rails/component_mount_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ class ComponentMountTest < ActionDispatch::IntegrationTest
2424
end
2525
end
2626

27+
test '#react_component allows camelize_props to be passed in as an option' do
28+
React::Rails::ComponentMount.camelize_props_switch = false
29+
helper = React::Rails::ComponentMount.new
30+
html = helper.react_component('Foo', {foo_bar: 'value'}, camelize_props: true)
31+
expected_props = %w(data-react-class="Foo" data-react-props="{&quot;fooBar&quot;:&quot;value&quot;}")
32+
expected_props.each do |segment|
33+
assert html.include?(segment)
34+
end
35+
end
36+
2737
test '#react_component accepts React props with camelize_props containing nested arrays' do
2838
React::Rails::ComponentMount.camelize_props_switch = true
2939
helper = React::Rails::ComponentMount.new

0 commit comments

Comments
 (0)