React::Rails::HotLoader
Reload React.js components with Ruby on Rails & react-rails.
Installation
Add this line to your application's Gemfile:
gem 'react-rails-hot-loader'
And then execute:
$ bundle
Or install it yourself as:
$ gem install react-rails-hot-loader
Usage
- Add an initializer (eg,
app/config/initializers/react_rails_hot_loader.rb):
if Rails.env.development?
React::Rails::HotLoader.start()
end
- Include the Javascript (eg, in
app/assets/javascripts/application.js):
//= require react_rails_hot_loader
(When not Rails.env.development?, this requires an empty file, so don't worry about leaving it in production deploys.)
- Restart your development server
Doeses & Doesn'ts
react-rails-hot-loader ...
- does set up a WebSocket server & client
- does reload JS assets when they change (from
/app/assets/javascripts/*.js*) - does remount components (via
ReactRailsUJS) after reloading assets - does preserve state & props (because
React.renderdoes that out of the box) - doesn't reload Rails view files (
html,erb,slim, etc) - doesn't reload CSS (although that could be fixed)
TODO
- Handle Passenger occasionally killing background threads :(
- Replace pinging with file watching
- Test Server, AssetPath
- Add
rails g react-rails-hot-loader:installto add initializer and JS
License
The gem is available as open source under the terms of the MIT License.