Adding a simple react component written in ruby to your Rails app
suggest change- Add the
hyperloopgem to the Gemfile of a Rails 4.0–5.1 app. - Run
bundle install. - Add the Hyperloop manifest to the application JavaScript file:
// app/assets/javascripts/application.js
// ...
//= require hyperloop-loader
- Create React components under
app/hyperloop/components:
# app/hyperloop/components/hello_world.rb
class HelloWorld < Hyperloop::Component
after_mount do
every(1.second) { mutate.current_time(Time.now) }
end
render do
"Hello World! The time is now: #{state.current_time}"
end
end
- Components act like views. Mount one with
render_componentin a controller:
# somewhere in a controller:
# ...
def hello_world
render_component # renders HelloWorld based on the action name
end
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents