Use React and JSX in Ruby on Rails
This blog site has been archived. Go to react.dev/blog to see the recent posts.
Today we’re releasing a gem to make it easier to use React and JSX in Ruby on Rails applications: react-rails.
This gem has 2 primary purposes:
- To package
react.jsin a way that’s easy to use and easy to update.
- To allow you to write JSX without an external build step to transform that into JS.
react.js available for use client-side, simply add
react to your manifest, and declare the variant you’d like to use in your environment. When you use
:production, the minified and optimized
react.min.js will be used instead of the development version. For example:
# config/environments/development.rb MyApp::Application.configure do config.react.variant = :development # use :production in production.rb end
When you name your file with
react-rails will automatically try to transform that file. For the time being, we still require that you include the docblock at the beginning of the file. For example, this file will get transformed on request.
/** @jsx React.DOM */ React.renderComponent(<MyComponent/>, document.getElementById('example'))
react-rails takes advantage of the asset pipeline that was introduced in Rails 3.1. A very important part of that pipeline is the
assets:precompile Rake task.
react-rails will ensure that your JSX files will be transformed into regular JS before all of your assets are minified and packaged.
Installation follows the same process you’re familiar with. You can install it globally with
gem install react-rails, though we suggest you add the dependency to your