From 7e824c875a73cc78faf9b42c5cd2f58e13722d5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20O=E2=80=99Shannessy?= Date: Tue, 30 Jul 2013 15:25:44 -0700 Subject: [PATCH] react-rails blog post --- ...7-30-use-react-and-jsx-in-ruby-on-rails.md | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 _posts/2013-07-30-use-react-and-jsx-in-ruby-on-rails.md diff --git a/_posts/2013-07-30-use-react-and-jsx-in-ruby-on-rails.md b/_posts/2013-07-30-use-react-and-jsx-in-ruby-on-rails.md new file mode 100644 index 00000000..608da381 --- /dev/null +++ b/_posts/2013-07-30-use-react-and-jsx-in-ruby-on-rails.md @@ -0,0 +1,54 @@ +--- +title: "Use React and JSX in Ruby on Rails" +layout: post +author: Paul O'Shannessy +--- + +Today we're releasing a gem to make it easier to use React and JSX in Ruby on Rails applications: [react-rails](https://github.com/facebook/react-rails). + + +This gem has 2 primary purposes: + +1. To package `react.js` in a way that's easy to use and easy to update. +2. To allow you to write JSX without an external build step to transform that into JS. + + +## Packaging react.js + +To make `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: + +```ruby +# config/environments/development.rb + +MyApp::Application.configure do + config.react.variant = :development + # use :production in production.rb +end +``` + +```js +// app/assets/javascript/application.js + +//= require react +``` + + +## Writing JSX + +When you name your file with `myfile.js.jsx`, `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. + +```js +/** @jsx React.DOM */ +React.renderComponent(, document.body) +``` + + +## Asset Pipeline + +`react-rails` takes advantage of the [asset pipeline](http://guides.rubyonrails.org/asset_pipeline.html) 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 + +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 `Gemfile` directly. +