Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ MyApp::Application.configure do
end
```

or when mounting:

```erb
<%= react_component('HelloMessage', {name: 'John'}, {camelize_props: true}) %>
```

### Rendering components instead of views

Components can also be prerendered directly from a controller action with the custom `component` renderer. For example:
Expand Down
4 changes: 2 additions & 2 deletions lib/react/rails/component_mount.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def teardown(env)
# on the client.
def react_component(name, props = {}, options = {}, &block)
options = {:tag => options} if options.is_a?(Symbol)
if camelize_props_switch
if camelize_props_switch || options[:camelize_props]
props = React.camelize_props(props)
end

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

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

content_tag(html_tag, '', html_options, &block)
end
Expand Down
10 changes: 10 additions & 0 deletions test/react/rails/component_mount_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ class ComponentMountTest < ActionDispatch::IntegrationTest
end
end

test '#react_component allows camelize_props to be passed in as an option' do
React::Rails::ComponentMount.camelize_props_switch = false
helper = React::Rails::ComponentMount.new
html = helper.react_component('Foo', {foo_bar: 'value'}, camelize_props: true)
expected_props = %w(data-react-class="Foo" data-react-props="{&quot;fooBar&quot;:&quot;value&quot;}")
expected_props.each do |segment|
assert html.include?(segment)
end
end

test '#react_component accepts React props with camelize_props containing nested arrays' do
React::Rails::ComponentMount.camelize_props_switch = true
helper = React::Rails::ComponentMount.new
Expand Down