Skip to content

Create render_react_component controller helper #118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from

Conversation

garbles
Copy link
Contributor

@garbles garbles commented Nov 20, 2014

render_react_component allows you to render a React component in place of a typical rails template from app/views.

Instead of creating a view that only renders a component, e.g.

<!-- app/views/todos/index.html.erb -->

<%= react_component "TodoList", {:todos => @todos}, :prerender => true %>

You can create call the helper from the controller action instead, e.g.

# app/controllers/todos_controller.rb

class TodosController < ApplicationController
  def index
    @todos = %w{todo1 todo2 todo3}
    render_react_component "TodoList", {:todos => @todos}, :prerender => true
  end
end

@facebook-github-bot
Copy link

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla - and if you have received this in error or have any questions, please drop us a line at [email protected]. Thanks!

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@garbles garbles force-pushed the add-render-react-component branch from bd5a131 to b71b1c9 Compare November 20, 2014 04:23
end
end

ActionController::Base.send(:include, React::Rails::ControllerHelper) if defined?(Rails)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this go in an active support hook?

See for example how react-rails includes the view helper, also here's an ActionController example

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Done here 2cc9748

@rmosolgo
Copy link
Member

👍 Although I don't have a place to use it, I think this is a fitting feature. It would be nice if took a :layout option (or maybe even :status?) like render does.

Also, before merging, it should be added to the readme!

@__react_component_args = args
@__react_component_block = block

render inline: '<%= react_component(*@__react_component_args, &@__react_component_block) %>', layout: current_layout
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can just merge in passed options and send to render, so someone can easily override layout/ status or other render options.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vipulnsward
Copy link
Contributor

I think this is pretty useful, as more people are using complete view contents to be served from a React Component, although this deviates from semantics of render and hides render call under magical render_react_component. A good alternative would be to just support:

render inline: react_component('TodoList'), layout: 'my_layout'

@rmosolgo
Copy link
Member

@vipulnsward that is nice, react_component(...) in the controller keeps the API surface area down too, good for my brain :)

@garbles garbles force-pushed the add-render-react-component branch from 2cc9748 to 3073fe6 Compare December 28, 2014 02:44
@garbles
Copy link
Contributor Author

garbles commented Dec 28, 2014

@vipulnsward That's an idea, but I'd rather not have to specify the layout for every action. By default rendering inline will not use a layout.

@garbles garbles mentioned this pull request Dec 28, 2014
@garbles garbles force-pushed the add-render-react-component branch from 4e77a12 to 69625fb Compare December 29, 2014 21:20
@zpao
Copy link
Member

zpao commented Dec 29, 2014

Are there other "renderers" out there that we can mimic for API? Personally I also find piggybacking on Rails' render a bit nicer and gives a bit more option value.

I'd rather not have to specify the layout for every action

Can't you just specify it once in the controller? layout false Or even layout false, :except => ['index']

@garbles
Copy link
Contributor Author

garbles commented Dec 30, 2014

Are there other "renderers" out there that we can mimic for API?

@zpao I could probably make something like that work. Would rather use a custom renderer than calling render inline: react_component(...), layout: .... What about this:

render component: "TodoList", data: {...}, options: { prerender: true }, layout: ..., status: ...

(layout and status are optional, of course)

Can't you just specify it once in the controller? layout false Or even layout false, :except => ['index']

:inline will ignore the layout unless explicitly stated with the :layout option AFAIK.

/cc @rmosolgo @vipulnsward

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants