# ReactOnRails [![License](https://img.shields.io/badge/license-mit-green.svg)](./LICENSE.md) [![Build Status](https://travis-ci.org/shakacode/react_on_rails.svg?branch=master)](https://travis-ci.org/shakacode/react_on_rails) [![Codeship Status for shakacode/react_on_rails](https://app.codeship.com/projects/cec6c040-971f-0134-488f-0a5146246bd8/status?branch=master)](https://app.codeship.com/projects/187011) [![Gem Version](https://badge.fury.io/rb/react_on_rails.svg)](https://badge.fury.io/rb/react_on_rails) [![npm version](https://badge.fury.io/js/react-on-rails.svg)](https://badge.fury.io/js/react-on-rails) [![Code Climate](https://codeclimate.com/github/shakacode/react_on_rails/badges/gpa.svg)](https://codeclimate.com/github/shakacode/react_on_rails) [![Coverage Status](https://coveralls.io/repos/shakacode/react_on_rails/badge.svg?branch=master&service=github)](https://coveralls.io/github/shakacode/react_on_rails?branch=master) React on Rails integrates Rails with (server rendering of) Facebook's [React](https://github.com/facebook/react) front-end framework. Intersted in optimizing your webpack setup for React on Rails including code splitting with react-router v4, webpack v4, and react-loadable? [Contact me](mailto:justin@shakacode.com). # Intro ## Project Objective To provide an opinionated and optimal framework for integrating Ruby on Rails with React via the [**Webpacker**](https://github.com/rails/webpacker) gem especially in regards to React Server Rendering. ## Features and Why React on Rails? Given that rails/webpacker gem already provides basic React integration, why would you use "React on Rails"? 1. Server rendering, often used for SEO crawler indexing and UX performance, is not offered by rails/webpacker. 1. The easy passing of props directly from your Rails view to your React components rather than having your Rails view load and then make a separate 1request to your API. 1. [Redux](https://github.com/reactjs/redux) and [React Router](https://github.com/reactjs/react-router) integration. 1. [Internationalization (I18n) and (localization)](https://github.com/shakacode/react_on_rails/blob/master/docs/basics/i18n.md) 1. [RSpec Test Helpers Configuration](docs/basics/rspec-configuration.md) to ensure your Webpack bundles are ready for tests. 1. A supportive community. This [web search shows how live public sites are using React on Rails](https://publicwww.com/websites/%22react-on-rails%22++-undeveloped.com/). 1. [Reason ML Support](https://github.com/shakacode/reason-react-on-rails-example) See the [react-webpack-rails-tutorial](https://github.com/shakacode/react-webpack-rails-tutorial) for an example of a live implementation and code. ## ShakaCode Forum Premium Content _Requires creating a free account._ * [How to use different versions of a file for client and server rendering](https://forum.shakacode.com/t/how-to-use-different-versions-of-a-file-for-client-and-server-rendering/1352) ## React on Rails Pro and ShakaCode Pro Support React on Rails Pro provides Node server rendering and other performance enhancements for React on Rails. ![2018-09-11_10-31-11](https://user-images.githubusercontent.com/1118459/45467845-5bcc7400-b6bd-11e8-91e1-e0cf806d4ea4.png) * [HVMN Testimonial, Written by Paul Benigeri, October 12, 2018](./docs/testimonials/hvmn.md) * [HVMN’s 90% Reduction in Server Response Time from React on Rails Pro](https://blog.shakacode.com/hvmns-90-reduction-in-server-response-time-from-react-on-rails-pro-eb08226687db) * [Egghead React on Rails Pro Deployment Highlights](https://github.com/shakacode/react_on_rails/wiki/Egghead-React-on-Rails-Pro-Deployment-Highlights) For more information, see the [React on Rails Pro Docs](https://github.com/shakacode/react_on_rails/wiki). The [ShakaCode Pro Support Plan](http://www.shakacode.com/work/shakacode-pro-support.pdf) can help you with: * Optimizing your webpack setup to Webpack v4 for React on Rails including code splitting with react-router v4, webpack v4, and react-loadable. * Upgrading your app to use the current Webpack setup that skips the Sprockets asset pipeline. * Better performance client and server side. * Efficiently migrating from [Angular to React](https://www.shakacode.com/services/angular-to-react/). * Best practices based on over four years of React on Rails experience. * Using [Reason](https://reasonml.github.io/) with (or without) React on Rails. ShakaCode can also help you with your custom software development needs. We specialize in marketplace and e-commerce applications that utilize both Rails and React. Because we own [HawaiiChee.com](https://www.hawaiichee.com), we can leverage that code for your app! The article [Why Hire ShakaCode?](https://blog.shakacode.com/can-shakacode-help-you-4a5b1e5a8a63#.jex6tg9w9) provides additional details about our projects. Please [email me (Justin Gordon), the creator of React on Rails](mailto:justin@shakacode.com), to see if I can help you or if you want an invite to our private Slack room for ShakaCode. ## Testimonials for Hiring ShakaCode and our "Pro Support" [HVMN Testimonial, Written by Paul Benigeri, October 12, 2018](./docs/testimonials/hvmn.md) > The price we paid for the consultation + the React on Rails pro license has already been made back a couple of times from hosting fees alone. The entire process was super hands off, and our core team was able to focus on shipping new feature during that sprint. From Kyle Maune of Cooper Aerial, May 4, 2018 ![image](https://user-images.githubusercontent.com/1118459/40891236-9b0b406e-671d-11e8-80ee-c026dbd1d5a2.png) From Joel Hooks, Co-Founder, Chief Nerd at [egghead.io](https://egghead.io/), January 30, 2017: ![2017-01-30_11-33-59](https://cloud.githubusercontent.com/assets/1118459/22443635/b3549fb4-e6e3-11e6-8ea2-6f589dc93ed3.png) For more testimonials, see [Live Projects](PROJECTS.md) and [Kudos](./KUDOS.md). ---- # Docs **Consider browsing this on our [documentation Gitbook](https://shakacode.gitbooks.io/react-on-rails/content/).** ## Prerequisites React on Rails supports older versions of Rails back to 3.x. Rails/webpacker requires version 4.2+. ## Getting Started Note, the best way to understand how to use ReactOnRails is to study a few simple examples. You can do a quick demo setup, either on your existing app or on a new Rails app. 1. Do the quick [tutorial](docs/tutorial.md). 2. Add React on Rails to an existing Rails app per [the instructions](docs/basics/installation-into-an-existing-rails-app.md). 3. Look at [spec/dummy](spec/dummy), a simple, no DB example. 3. Look at [github.com/shakacode/react-webpack-rails-tutorial](https://github.com/shakacode/react-webpack-rails-tutorial); it's a full featured example live at [www.reactrails.com](http://www.reactrails.com). ## Basic Installation *See also [the instructions for installing into an existing Rails app](docs/basics/installation-into-an-existing-rails-app.md).* 1. Create a new Rails app: ``````bash $ rails new my-app --webpack=react $ cd my-app `````` 2. Add the `react_on_rails` gem to Gemfile: ```ruby gem 'react_on_rails', '11.1.4' # Use the exact gem version to match npm version ``` 3. Install the `react_on_rails` gem: ```bash $ bundle install ``` 4. Commit this to git (or else you cannot run the generator unless you pass the option `--ignore-warnings`). 5. Run the generator: ```bash $ rails generate react_on_rails:install ``` 6. Start the app: ```bash $ rails s ``` 7. Visit http://localhost:3000/hello_world. ### Turning on server rendering With the code from running the React on Rails generator above: 1. Edit `app/views/hello_world/index.html.erb` and set `prerender` to `true`. 2. Refresh the page. Below is the line where you turn server rendering on by setting `prerender` to true: ```erb <%= react_component("HelloWorld", props: @hello_world_props, prerender: false) %> ``` ## Basic Usage ### Configuration * Configure `config/initializers/react_on_rails.rb`. You can adjust some necessary settings and defaults. See file [docs/basics/configuration.md](docs/basics/configuration.md) for documentation of all configuration options. * Configure `config/webpacker.yml`. If you used the generator and the default webpacker setup, you don't need to touch this file. If you are customizing your setup, then consult the [spec/dummy/config/webpacker.yml](./spec/dummy/config/webpacker.yml) example * Set `compile: false` for all envs * Your `public_output_path` must match your Webpack configuration for `output` of your bundles. * Only set `cache_manifest` to `true` in your production env. ## Including your React Component on your Rails Views - React component are rendered via your Rails Views. Here's an ERB sample: ```erb <%= react_component("HelloWorld", props: @some_props) %> ``` - **Server-Side Rendering**: Your react component is first rendered into HTML on the server. Use the **prerender** option: ```erb <%= react_component("HelloWorld", props: @some_props, prerender: true) %> ``` - The `component_name` parameter is a string matching the name you used to expose your React component globally. So, in the above examples, if you had a React component named "HelloWorld", you would register it with the following lines: ```js import ReactOnRails from 'react-on-rails'; import HelloWorld from './HelloWorld'; ReactOnRails.register({ HelloWorld }); ``` Exposing your component in this way is how React on Rails is able to reference your component from a Rails view. You can expose as many components as you like, as long as their names do not collide. See below for the details of how you expose your components via the react_on_rails webpack configuration. - `@some_props` can be either a hash or JSON string. This is an optional argument assuming you do not need to pass any options (if you want to pass options, such as `prerender: true`, but you do not want to pass any properties, simply pass an empty hash `{}`). This will make the data available in your component: ```ruby # Rails View <%= react_component("HelloWorld", props: { name: "Stranger" }) %> ``` - This is what your HelloWorld.js file might contain. The railsContext is always available for any parameters that you _always_ want available for your React components. It has _nothing_ to do with the concept of the [React Context](https://reactjs.org/docs/context.html). See [Generator Functions and the RailsContext](docs/basics/generator-functions-and-railscontext.md) for more details on this topic. ```js import React from 'react'; export default (props, railsContext) => { return (