# 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](https://www.shakacode.com/react-on-rails/docs/guides/tutorial/). 2. Add React on Rails to an existing Rails app per [the instructions](https://www.shakacode.com/react-on-rails/docs/guides/installation-into-an-existing-rails-app/). 3. Look at [spec/dummy](https://github.com/shakacode/react_on_rails/tree/master/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://reactrails.com). ## Basic Installation These steps assume that you've got a rails application with webpacker (e.g. one generated using `rails new my_app --javascript=webpack`). If your application is not yet set up to use webpacker, please see [the instructions for installing into an existing Rails app](https://www.shakacode.com/react-on-rails/docs/guides/installation-into-an-existing-rails-app/).* 1. Add the `shakapacker` and `react_on_rails` gem to Gemfile: ```bash bundle add shakapacker --strict bundle add react_on_rails --strict ``` 2. Run installation command for webpacker: ```bash rails webpacker:install ``` 3. Commit this to git (or else you cannot run the generator unless you pass the option `--ignore-warnings`). 3. Run the generator: ```bash rails generate react_on_rails:install ``` 4. Start the app: - Run `./bin/dev` for HMR - Run `./bin/dev-static` for statically created bundles (no HMR) 5. 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 the `prerender` option to `true`. You may need to use `Node` as your js runtime environment by setting `EXECJS_RUNTIME=Node` into your environment variables. 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: true) %> ``` Note, if you got an error in your console regarding "ReferenceError: window is not defined", then you need to edit `config/webpacker.yml` and set `hmr: false` and `inline: false`. See [rails/webpacker PR 2644](https://github.com/rails/webpacker/pull/2644) for a fix for this issue. ## Basic Usage ### Configuration * Configure `config/initializers/react_on_rails.rb`. You can adjust some necessary settings and defaults. See file [docs/basics/configuration.md](https://www.shakacode.com/react-on-rails/docs/guides/configuration/) 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](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy/config/webpacker.yml) example or the official default [webpacker.yml](https://github.com/rails/webpacker/blob/master/lib/install/config/webpacker.yml). * Most apps should rely on the rails/webpacker setup for Webpack. v6 of rails/webpacker includes support for v5 of webpack. ## 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. You may call `ReactOnRails.register` many times. - `@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 [Render-Functions and the RailsContext](https://www.shakacode.com/react-on-rails/docs/guides/render-functions-and-railscontext/) for more details on this topic. ```js import React from 'react'; export default (props, railsContext) => { // Note wrap in a function to make this a React function component return () => (