facebook/react-rails — pull request #17 by quark-zju (2013-12-03 00:34:30+00:00)
Title: View helper to render react component

Let's discuss here and if you like the idea, we can talk about details and I will convert this issue into a PR.

To render a react component, currently it requires a `

` and a line of js calling
`React.renderComponent`. Developers need also take care of JSON and div id, etc. This feels not elegant in Ruby world.

Inspired by other helper method like `content_tag`, `select_tag` etc. I think it may be a good idea to make one for react. So a developer can simply write this in view template:

```ruby
# Take the first example at http://facebook.github.io/react/index.html
render_react_component 'HelloMessage', :name => 'John'
```

I have a proposed implement:

```ruby
def render_react_component(name, params = {}, html_options = {})
html_options.reverse_merge!{:id => "#{name}-#{SecureRandom::uuid}"}

html = content_tag(:div, '', html_options)
html << javascript_tag(
"$(document).on('ready page:load',function(){" \
"React.renderComponent(" \
"#{name}(#{params.to_json})," \
"document.getElementById('#{html_options[:id]}')" \
");" \
"});",
defer: 'defer'
)
html
end
```

Future work could be: optionally use `React.renderComponentToString` to render initial html at server side.

Thoughts?