Skip to content

Commit 0a71a94

Browse files
author
Robert Mosolgo
committed
Merge pull request #409 from ZhangHanDong/master
Snake-Case key to Camel-Case key for props when server render
2 parents c463b8e + 1bf354e commit 0a71a94

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,14 @@ end
206206
- On MRI, you'll get a deadlock with `pool_size` > 1
207207
- If you're using JRuby, you can increase `pool_size` to have real multi-threaded rendering.
208208

209+
You can configure camelize_props option and pass props with an underscored hash from rails but get a camelized hash in jsx :
210+
211+
```ruby
212+
MyApp::Application.configure do
213+
config.react.camelize_props = true #default false
214+
end
215+
```
216+
209217
### Rendering components instead of views
210218

211219
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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class ComponentMount
99
include ActionView::Helpers::TagHelper
1010
include ActionView::Helpers::TextHelper
1111
attr_accessor :output_buffer
12+
mattr_accessor :camelize_props_switch
1213

1314
# ControllerLifecycle calls these hooks
1415
# You can use them in custom helper implementations
@@ -23,6 +24,7 @@ def teardown(env)
2324
# on the client.
2425
def react_component(name, props = {}, options = {}, &block)
2526
options = {:tag => options} if options.is_a?(Symbol)
27+
props = camelize_props_key(props) if camelize_props_switch
2628

2729
prerender_options = options[:prerender]
2830
if prerender_options
@@ -41,6 +43,15 @@ def react_component(name, props = {}, options = {}, &block)
4143

4244
content_tag(html_tag, '', html_options, &block)
4345
end
46+
47+
private
48+
49+
def camelize_props_key(props)
50+
return props unless props.is_a?(Hash)
51+
props.inject({}) do |h, (k,v)|
52+
h[k.to_s.camelize(:lower)] = v.is_a?(Hash) ? camelize_props_key(v) : v; h
53+
end
54+
end
4455
end
4556
end
4657
end

lib/react/rails/railtie.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class Railtie < ::Rails::Railtie
99
config.react.addons = false
1010
config.react.jsx_transform_options = {}
1111
config.react.jsx_transformer_class = nil # defaults to BabelTransformer
12+
config.react.camelize_props = false # pass in an underscored hash but get a camelized hash
13+
1214
# Server rendering:
1315
config.react.server_renderer_pool_size = 1 # increase if you're on JRuby
1416
config.react.server_renderer_timeout = 20 # seconds
@@ -31,6 +33,7 @@ class Railtie < ::Rails::Railtie
3133

3234
app.config.react.view_helper_implementation ||= React::Rails::ComponentMount
3335
React::Rails::ViewHelper.helper_implementation_class = app.config.react.view_helper_implementation
36+
React::Rails::ComponentMount.camelize_props_switch = app.config.react.camelize_props
3437

3538
ActiveSupport.on_load(:action_controller) do
3639
include ::React::Rails::ControllerLifecycle

test/react/rails/component_mount_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ class ComponentMountTest < ActionDispatch::IntegrationTest
1313
end
1414
end
1515

16+
test '#react_component accepts React props with camelize_props ' do
17+
React::Rails::ComponentMount.camelize_props_switch = true
18+
helper = React::Rails::ComponentMount.new
19+
html = helper.react_component('Foo', {foo_bar: 'value'})
20+
expected_props = %w(data-react-class="Foo" data-react-props="{&quot;fooBar&quot;:&quot;value&quot;}")
21+
expected_props.each do |segment|
22+
assert html.include?(segment)
23+
end
24+
end
25+
1626
test '#react_component accepts jbuilder-based strings as properties' do
1727
jbuilder_json = Jbuilder.new do |json|
1828
json.bar 'value'

0 commit comments

Comments
 (0)