Code Monkey home page Code Monkey logo

react-rails's Introduction

react-rails

react-rails is a ruby gem which makes it easier to use React and JSX in your Ruby on Rails application.

This is done in 2 ways:

  1. making it easy to include react.js as part of your dependencies in application.js.
  2. transforming JSX into regular JS on request, or as part of asset precompilation.

Installation

We're specifically targeting versions of Ruby on Rails which make use of the asset pipeline, which means Rails 3.1+.

As with all gem dependencies, we strongly recommend adding react-rails to your Gemfile and using bundler to manage your application's dependencies.

# Gemfile

gem 'react-rails', '~> 0.4.1.0'

Usage

react.js

In order to use React client-side in your application, you must make sure the browser requests it. One way to do that is to drop react.js into app/assets/javascript/ and by default your applcation manifest will pick it up. There are downsides to this approach, so we made it even easier. Once you have react-rails installed, you can just require react directly in your manifest:

You can require it in your manifest:

// app/assets/application.js

//= require react

Alternatively, you can include it directly as a separate script tag:

# app/views/layouts/application.erb.html

<%= javascript_include_tag "react" %>

JSX

To transform your JSX into JS, simply create .js.jsx files, and ensure that the file has the /** @jsx React.DOM */ docblock. These files will be transformed on request, or precompiled as part of the assets:precompile task.

Configuring

There are 2 variants available. :development gives you the unminified version of React. This provides extra debugging and error prevention. :production gives you the minified version of React which strips out comments and helpful warnings, and minifies.

# config/environments/development.rb
MyApp::Application.configure do
  config.react.variant = :development
end

# config/environments/production.rb
MyApp::Application.configure do
  config.react.variant = :production
end

react-rails's People

Contributors

zpao avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.