Integrating React.js with Rails Using Hyperloop
This topic covers integrating React.js with Rails using the Hyperloop gem
Other approaches not covered here are using the react-rails or react_on_rails gems.
Adding a simple react component (written in ruby) to your Rails app
Section titled “Adding a simple react component (written in ruby) to your Rails app”- Add the hyperloop gem to your rails (4.0 - 5.1) Gemfile
bundle install
// app/assets/javascripts/application.js ... //= hyperloop-loader
# 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
# somewhere in a controller:
...
def hello_world
render_component # renders HelloWorld based on method name
end
Callbacks
Section titled “Callbacks”Declaring component parameters (props)
Section titled “Declaring component parameters (props)”HTML Tags
Section titled “HTML Tags”Event Handlers
Section titled “Event Handlers”States
Section titled “States”Note that states can be shared between components using Hyperloop::Stores
Remarks
Section titled “Remarks”Component classes simply generate the equivalent javascript component classes.
You can also access javascript components and libraries directly from your ruby component classes.
Hyperloop will “prerender” the view server side so your initial view will load just like ERB or HAML templates. Once loaded on the client react takes over and will incrementally update the DOM as state changes due to inputs from the user, HTTP requests or incoming web socket data.
Besides Components, Hyperloop has Stores to manage shared state, Operations to encapsulate isomorphic business logic, and Models which give direct access to your ActiveRecord models on the client using the standard AR syntax.
More info here: http://ruby-hyperloop.io/