Skip to content

Commit

Permalink
Make changes identified in PR
Browse files Browse the repository at this point in the history
  • Loading branch information
robwise committed Nov 11, 2015
1 parent 9c33884 commit 8f30521
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 79 deletions.
71 changes: 55 additions & 16 deletions README.md
Expand Up @@ -3,6 +3,8 @@
# React on Rails
React on Rails integrates Facebook's [React](https://github.com/facebook/react) front-end framework with Rails. Currently, both React v0.14 and v0.13 are supported. See the Rails on Maui [blog post](http://www.railsonmaui.com/blog/2014/10/03/integrating-webpack-and-the-es6-transpiler-into-an-existing-rails-project/) that started it all!

Be sure to see the [React Webpack Rails Tutorial Code](link) along with the live example at [www.reactrails.com](http://www.reactrails.com).

## Including your React Component in your Rails Views
Please see [Getting Started](#getting-started) for how to set up your Rails project for React on Rails if you have not already done so.

Expand All @@ -17,7 +19,7 @@ Please see [Getting Started](#getting-started) for how to set up your Rails proj
<%= react_component("HelloWorldApp", @some_props, prerender: true) %>
```

+ The `component_name` parameter is a string matching the name you used when globally exposing your React component. So, in the above examples, if you had a React component named "HelloWorld," you would set `window.HelloWorldApp = HelloWorld` in your JavaScript. 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.
+ The `component_name` parameter is a string matching the name you used when globally exposing your React component. So, in the above examples, if you had a React component named "HelloWorld," you would set `window.HelloWorldApp = HelloWorld` in your JavaScript. 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 or `{}` if you have no props. This will make the data available in your component:

```ruby
Expand All @@ -32,6 +34,7 @@ Please see [Getting Started](#getting-started) for how to set up your Rails proj
## Documentation

+ [Features](#features)
+ [Why Webpack?](#why-webpack)
+ [Getting Started](#getting-started)
+ [How it Works](#how-it-works)
- [Client-Side Rendering vs. Server-Side Rendering](#client-side-rendering-vs-server-side-rendering)
Expand Down Expand Up @@ -67,9 +70,25 @@ Like the [react-rails](https://github.com/reactjs/react-rails) gem, React on Rai
+ [Webpack dev server](https://webpack.github.io/docs/webpack-dev-server.html) with [hot module replacement](https://webpack.github.io/docs/hot-module-replacement-with-webpack.html)
+ [Webpack optimization functionality](https://github.com/webpack/docs/wiki/optimization)
+ _**(Coming Soon)**_ [React Router](https://github.com/rackt/react-router)
+ *([Gem Pull Request](https://github.com/shakacode/react_on_rails/pull/68))*
+ *([Tutorial Pull Request](https://github.com/shakacode/react-webpack-rails-tutorial/pull/128))*

See the [react-webpack-rails-tutorial](https://github.com/justin808/react-webpack-rails-tutorial/) for an example of a live implementation and code.

## Why Webpack?

Webpack is used for 2 purposes:

1. Generate several JavaScript "bundles" for inclusion in `application.js`.
2. Providing the Webpack Dev Server for quick prototyping of components without needing to refresh your browser to see updates.

This usage of webpack fits neatly and simply into the existing Rails sprockets system and you can include React components on a Rails view with a simple helper.

Compare this to some alternative approaches for SPAs (Single Page Apps) that utilize Webpack and Rails. They will use a separate node server to distribute web pages, JavaScript assets, CSS, etc., and will still use Rails as an API server. A good example of this is our ShakaCode team member Alex's article [
Universal React with Rails: Part I](https://medium.com/@alexfedoseev/isomorphic-react-with-rails-part-i-440754e82a59).

We're definitely not doing that. With react_on_rails, webpack is mainly generating a nice JavaScript file for inclusion into application.js. We're going to KISS. And that's all relative given how much there is to get right in an enterprise class web application.

## Getting Started
1. Add the following to your Gemfile and bundle install:

Expand Down Expand Up @@ -108,12 +127,18 @@ In most cases, you should use the `prerender: false` (default behavior) with the

Note that **server-rendering requires globally exposing your components by setting them to `global`, not `window`** (as is the case with client-rendering). If using the generator, you can pass the `--server-rendering` option to configure your application for server-side rendering.

In the following screenshot you can see the actual HTML rendered for a side-by-side comparison of a React component left as JavaScript for the client to render followed by the same component rendered on the server to HTML along with any console error messages generated:
In the following screenshot you can see the 3 parts of react_on_rails rendering:

1. Inline JavaScript to "client-side" render the React component.
2. The wrapper div `<div id="HelloWorld-react-component-0">` enclosing the server-rendered HTML for the React component
3. Additional JavaScript to console log any messages, such as server rendering errors.

**Note**: If server rendering is not used (prerender: false), then the major difference is that the HTML rendered contains *only* the outer div: `<div id="HelloWorld-react-component-0"/>`

![Comparison of a normal React Component with its server-rendered version](https://cloud.githubusercontent.com/assets/1118459/10157268/41435186-6624-11e5-9341-6fc4cf35ee90.png)

### Building the Bundles
Each time you change your client code, you will need to re-generate the bundles. The included Foreman `Procfile.dev` will take care of this for you by watching your JavaScript code files for changes. Simply run `foreman start -f Procfile.dev`.
Each time you change your client code, you will need to re-generate the bundles (the webpack-created JavaScript files included in application.js). The included Foreman `Procfile.dev` will take care of this for you by watching your JavaScript code files for changes. Simply run `foreman start -f Procfile.dev`.

### Globally Exposing Your React Components
Place your JavaScript code inside of the provided `client/app` folder. Use modules just as you would when using webpack alone. The difference here is that instead of mounting React components directly to an element using `React.render`, you **expose your components globally and then mount them with helpers inside of your Rails views**.
Expand All @@ -134,31 +159,40 @@ Once the bundled files have been generated in your `app/assets/javascripts/gener

This is how you actually render the React components you exposed to `window` inside of `clientGlobals` (and `global` inside of `serverGlobals` if you are server rendering).

#### react_component
`react_component(component_name, props = {}, options = {})`

+ **react_component_name:** Can be a React component, created using a ES6 class, or `React.createClass`, or a generator function that returns a React component.
+ **props:** Ruby Hash which contains the properties to pass to the react object
+ **props:** Ruby Hash which contains the properties to pass to the react object, or a JSON string. If you pass a string, we'll escape it for you.
+ **options:**
+ **generator_function:** default is false, set to true if you want to use a generator function rather than a React Component.
+ **generator_function:** default is false, set to true if you want to use a generator function rather than a React Component. Why would you do this? For example, you may want the ability to use the pass-in-props to initialize a redux store or setup react-router. Or you may want to return different components depending on what's in the props.
+ **prerender:** enable server-side rendering of component. Set to false when debugging!
+ **trace:** set to true to print additional debugging information in the browser. Defaults to true for development, off otherwise.
+ **replay_console:** Default is true. False will disable echoing server-rendering logs to the browser. While this can make troubleshooting server rendering difficult, so long as you have the default configuration of logging_on_server set to true, you'll still see the errors on the server.
+ Any other options are passed to the content tag, including the id

`def server_render_js(js_expression, options = {})`
#### server_render_js
`server_render_js(js_expression, options = {})`

+ js_expression, like 2 + 3, and not a block of js code. If you have more than one line that needs to be executed, wrap it in an IIFE. JS exceptions will be caught and console messages handled properly
+ Currently the only option you may pass is `replay_console` (boolean)

This is a helper method that takes any JavaScript expression and returns the output from evaluating it. If you have more than one line that needs to be executed, wrap it in an IIFE. JS exceptions will be caught and console messages handled properly.

## Generator
The `react_on_rails:install` generator combined with the example pull requests of generator runs will get you up and running efficiently. There's a fair bit of setup with integrating Webpack with Rails.

Run `rails generate react_on_rails:install --help` for descriptions of all available options:

```
Usage:
rails generate react_on_rails:install [options]
Options:
-R, [--redux], [--no-redux] # Setup Redux files
-S, [--server-rendering], [--no-server-rendering] # Configure for server-side rendering of webpack JavaScript
-L, [--skip-linters], [--no-skip-linters] # Don't install linter files
-R, [--redux], [--no-redux] # Install Redux gems and Redux version of Hello World Example
-S, [--server-rendering], [--no-server-rendering] # Add necessary files and configurations for server-side rendering
-j, [--skip-js-linters], [--no-skip-js-linters] # Skip installing JavaScript linting files
-L, [--ruby-linters], [--no-ruby-linters] # Install ruby linting files, tasks, and configs
Runtime options:
-f, [--force] # Overwrite files that already exist
Expand All @@ -170,7 +204,7 @@ Description:
Create react on rails files for install generator.
```

We have a repo showing the results of running the generator with various combinations of options, each combination on its own branch: [Generator Results](https://github.com/shakacode/react_on_rails-generator-results-1-0-0/pulls).
For a clear example of what each generator option will do, see our generator results repo: [Generator Results](https://github.com/shakacode/react_on_rails-generator-results-1-0-0/pulls). Each pull request shows a git "diff" that highlights the changes that the generator has made.

### Understanding the Organization of the Generated Client Code
The generated client code follows our organization scheme. Each unique set of functionality, is given its own folder inside of `client/app/bundles`. This encourages for modularity of DOMAINS.
Expand All @@ -186,10 +220,10 @@ You may also notice the `app/lib` folder. This is for any code that is common be
### Redux
If you have used the `--redux` generator option, you will notice the familiar additional redux folders in addition to the aforementioned folders. The Hello World example has also been modified to use Redux.

In this organization paradigm, each bundle has its own store. We do not set a global store and then use partial stores based off of that. Again, this is for bundle code modularity and isolation. Note that if you want to reuse redux reducers across domains, then you will want to put the shared reducers under `/client/app/lib`.
Note the organizational paradigm of "bundles". These are like application domains and are used for grouping your code into webpack bundles, in case you decide to create different bundles for deployment. This is also useful for separating out logical parts of your application. The concept is that each bundle will have it's own Redux store. If you have code that you want to reuse across bundles, including components and reducers, place them under `/client/app/lib`.

### Using Images and Fonts
The generator has amended the folders created in `client/assets/` to Rails's asset path. We would that if you have any existing assets that you want to use with your client code that you should move them to these folders and use webpack as normal.
The generator has amended the folders created in `client/assets/` to Rails's asset path. We recommend that if you have any existing assets that you want to use with your client code, you should move them to these folders and use webpack as normal. This allows webpack's development server to have access to your assets, as it will not be able to see any assets in the Rails directories.

Alternatively, if you have many existing assets and don't wish to move them, you could consider creating symlinks from client/assets that point to your Rails assets folders inside of `app/assets/`. The assets there will then be visible to both Rails and webpack.

Expand All @@ -203,11 +237,11 @@ In the former case, the Rails server loads `bootstrap-sprockets`, provided by th

This allows for using Bootstrap in your regular Rails stylesheets. If you wish to customize any of the Bootstrap variables, you can do so via the `client/assets/stylesheets/_pre-bootstrap.scss` partial.

#### Bootstrap via Webpack HMR Dev Server
#### Bootstrap via Webpack Dev Server
When using the webpack dev server, which does not go through Rails, bootstrap is loaded via the [bootstrap-sass-loader](https://github.com/shakacode/bootstrap-sass-loader) which uses the `client/bootstrap-sass-config.js` file.

#### Keeping Custom Bootstrap Configurations Synced
Because the HMR dev server and Rails each load Bootstrap via a different file (explained in the two sections immediately above), any changes to the way components are loaded in one file must also be made to the other file in order to keep styling consistent between the two. For example, if an import is excluded in `_bootstrap-custom.scss`, the same import should be excluded in `bootstrap-sass-config.js` so that styling in the Rails server and the webpack dev server will be the same.
Because the webpack dev server and Rails each load Bootstrap via a different file (explained in the two sections immediately above), any changes to the way components are loaded in one file must also be made to the other file in order to keep styling consistent between the two. For example, if an import is excluded in `_bootstrap-custom.scss`, the same import should be excluded in `bootstrap-sass-config.js` so that styling in the Rails server and the webpack dev server will be the same.

### Linters
The React on Rails generator can add linters and their recommended accompanying configurations to your project. There are two classes of linters: ruby linters and JavaScript linters.
Expand All @@ -218,6 +252,8 @@ JavaScript linters are **enabled by default**, but can be disabled by passing th
##### Ruby Linters
Ruby linters are **disabled by default**, but can be enabled by passing the `--ruby-linters` flag when generating. These linters have been added to your Gemfile in addition to the the appropriate Rake tasks.

We really love using all the linters! Give them a try.

#### Running the Linters
To run the linters (runs all linters you have installed, even if you installed both Ruby and Node):

Expand Down Expand Up @@ -261,7 +297,10 @@ As you add more routes to your front-end application, you will need to make the
## Additional Reading
+ [Generated Client Code](docs/additional_reading/generated_client_code.md)
+ [Manual Installation](docs/additional_reading/manual_installation.md)
+ [Node Dependencies and NPM](docs/additional_reading/node_dependencies_and_npm)
+ [Node Dependencies and NPM](docs/additional_reading/node_dependencies_and_npm.md)
+ [Optional Configuration](docs/additional_reading/optional-configuration.md)
+ [Server Rendering Tips](docs/additional_reading/server-rendering-tips.md)
+ [Tips](docs/additional_reading/tips.md)

## Contributing
Bug reports and pull requests are welcome. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to our version of the [Contributor Covenant](contributor-covenant.org) code of conduct (see [CODE OF CONDUCT](docs/code_of_conduct.md)).
Expand All @@ -286,7 +325,7 @@ And based on the work of the [react-rails gem](https://github.com/reactjs/react-

## About [ShakaCode](http://www.shakacode.com/)

Visit [our forums!](http://forum.shakacode.com)
Visit [our forums!](http://forum.shakacode.com). We've got a [category dedicated to react_on_rails](http://forum.shakacode.com/c/rails/reactonrails).

If you're looking for consulting on a project using React and Rails, email us ([contact@shakacode.com](mailto: contact@shakacode.com))! You can also join our slack room for some free advice.

Expand Down

0 comments on commit 8f30521

Please sign in to comment.