Integrating React into Rails 5.2

Here is how to integrate React into Rails 5.2 project.

  1. Add webpacker and react-rails gems to your Gemfile.

  2. Run installers

$ rails webpacker:install       # OR (on rails version < 5.0) rake webpacker:install
$ rails webpacker:install:react # OR (on rails version < 5.0) rake webpacker:install:react
$ rails generate react:install
  1. Add JavaScript pack between the head tag in application.html.erb

<%= javascript_pack_tag 'application' %>

  1. Generate first component

rails g react:component HelloWorld greeting:string

  1. Add your component to your view

<%= react_component("HelloWorld", { greeting: "Hello" }) %>

  1. Run rails g react:install

You can now load your component in your Rails application.

You should see ‘Greeting: Hello!’ once you run your rails s and view your page.

Resource: React-Rails gem documentation here.

Published 26 Jun 2018

founder && full stack && ethereum developer.
Max Goodman on Twitter