Skip to content

Commit 1bf354e

Browse files
committed
add config.react.camelize_props
1 parent 517c2f6 commit 1bf354e

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

README.md

+8
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

+3-2
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,8 +24,8 @@ def teardown(env)
2324
# on the client.
2425
def react_component(name, props = {}, options = {}, &block)
2526
options = {:tag => options} if options.is_a?(Symbol)
26-
props = camelize_props_key(props)
27-
27+
props = camelize_props_key(props) if camelize_props_switch
28+
2829
prerender_options = options[:prerender]
2930
if prerender_options
3031
block = Proc.new{ concat React::ServerRendering.render(name, props, prerender_options) }

lib/react/rails/railtie.rb

+3
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

+10
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)