Skip to content

Commit 1ab76f5

Browse files
committed
rails generate react_on_rails:install
1 parent 58c7c55 commit 1ab76f5

File tree

14 files changed

+219
-3
lines changed

14 files changed

+219
-3
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,5 @@ gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
5050
gem "webpacker", "~> 5.1"
5151

5252
gem "react_on_rails", "12.0.1"
53+
54+
gem 'mini_racer', platforms: :ruby

Gemfile.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ GEM
8484
concurrent-ruby (~> 1.0)
8585
jbuilder (2.10.0)
8686
activesupport (>= 5.0.0)
87+
libv8 (7.3.492.27.1-x86_64-darwin-19)
8788
listen (3.2.1)
8889
rb-fsevent (~> 0.10, >= 0.10.3)
8990
rb-inotify (~> 0.9, >= 0.9.10)
@@ -98,6 +99,8 @@ GEM
9899
mimemagic (0.3.5)
99100
mini_mime (1.0.2)
100101
mini_portile2 (2.4.0)
102+
mini_racer (0.2.14)
103+
libv8 (> 7.3)
101104
minitest (5.14.1)
102105
msgpack (1.3.3)
103106
nio4r (2.5.2)
@@ -201,6 +204,7 @@ DEPENDENCIES
201204
capybara (>= 2.15)
202205
jbuilder (~> 2.7)
203206
listen (~> 3.2)
207+
mini_racer
204208
puma (~> 4.1)
205209
rails (~> 6.0.3, >= 6.0.3.1)
206210
react_on_rails (= 12.0.1)

Procfile.dev

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# You can run these commands in separate shells
2+
web: rails s -p 3000
3+
4+
# Next line runs a watch process with webpack to compile the changed files.
5+
# When making frequent changes to client side assets, you will prefer building webpack assets
6+
# upon saving rather than when you refresh your browser page.
7+
# Note, if using React on Rails localization you will need to run
8+
# `bundle exec rake react_on_rails:locale` before you run bin/webpack
9+
client: sh -c 'rm -rf public/packs/* || true && bin/webpack -w'

Procfile.dev-hmr

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Procfile for development using HMR
2+
3+
web: rails s -p 3000
4+
5+
# Note, hot and live reloading don't work with the default generator setup on
6+
# top of the rails/webpacker Webpack config with server rendering.
7+
# If you have server rendering enabled (prerender is true), you either need to
8+
# a. Ensure that you have dev_server.hmr and dev_server.inline BOTH set to false,
9+
# and you have this option in your config/initializers/react_on_rails.rb:
10+
# config.same_bundle_for_client_and_server = true
11+
# If you have either config/webpacker.yml option set to true, you'll see errors like
12+
# "ReferenceError: window is not defined" (if hmr is true)
13+
# "TypeError: Cannot read property 'prototype' of undefined" (if inline is true)
14+
# b. Skip using the webpack-dev-server. bin/webpack --watch is typically
15+
fast enough.
16+
# c. See the React on Rails README for a link to documentation for how to setup
17+
# SSR with HMR and React hot loading using the webpack-dev-server only for the
18+
# client bundles and a static file for the server bundle.
19+
20+
# Run the webpack-dev-server for client and maybe server files
21+
webpack-dev-server: bin/webpack-dev-server
22+
23+
# Keep the JS fresh for server rendering. Remove if not server rendering.
24+
# Especially if you have not configured generation of a server bundle without a hash.
25+
# as that will conflict with the manifest created by the bin/webpack-dev-server
26+
# rails-server-assets: SERVER_BUNDLE_ONLY=yes bin/webpack --watch
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
class HelloWorldController < ApplicationController
4+
layout "hello_world"
5+
6+
def index
7+
@hello_world_props = { name: "Stranger" }
8+
end
9+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import PropTypes from 'prop-types';
2+
import React, { useState } from 'react';
3+
4+
const HelloWorld = (props) => {
5+
const [name, setName] = useState(props.name);
6+
7+
return (
8+
<div>
9+
<h3>Hello, {name}!</h3>
10+
<hr />
11+
<form>
12+
<label htmlFor="name">
13+
Say hello to:
14+
<input id="name" type="text" value={name} onChange={(e) => setName(e.target.value)} />
15+
</label>
16+
</form>
17+
</div>
18+
);
19+
};
20+
21+
HelloWorld.propTypes = {
22+
name: PropTypes.string.isRequired, // this is passed from the Rails view
23+
};
24+
25+
export default HelloWorld;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import ReactOnRails from 'react-on-rails';
2+
3+
import HelloWorld from '../bundles/HelloWorld/components/HelloWorld';
4+
5+
// This is how react_on_rails can see the HelloWorld in the browser.
6+
ReactOnRails.register({
7+
HelloWorld,
8+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>Hello World</h1>
2+
<%= react_component("HelloWorld", props: @hello_world_props, prerender: false) %>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>ReactOnRailsWithWebpacker</title>
5+
<%= csrf_meta_tags %>
6+
<%= javascript_pack_tag 'hello-world-bundle' %>
7+
</head>
8+
9+
<body>
10+
<%= yield %>
11+
</body>
12+
</html>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
# See https://github.com/shakacode/react_on_rails/blob/master/docs/basics/configuration.md
4+
# for many more options.
5+
6+
ReactOnRails.configure do |config|
7+
# This configures the script to run to build the production assets by webpack. Set this to nil
8+
# if you don't want react_on_rails building this file for you.
9+
# If nil, then the standard rails/webpacker assets:precompile will run
10+
# config.build_production_command = nil
11+
12+
################################################################################
13+
################################################################################
14+
# TEST CONFIGURATION OPTIONS
15+
# Below options are used with the use of this test helper:
16+
# ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
17+
################################################################################
18+
19+
# If you are using this in your spec_helper.rb (or rails_helper.rb):
20+
#
21+
# ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
22+
#
23+
# with rspec then this controls what yarn command is run
24+
# to automatically refresh your webpack assets on every test run.
25+
#
26+
# Alternately, you can remove the `ReactOnRails::TestHelper.configure_rspec_to_compile_assets`
27+
# and set the config/webpacker.yml option for test to true.
28+
config.build_test_command = "RAILS_ENV=test bin/webpack"
29+
30+
################################################################################
31+
################################################################################
32+
# SERVER RENDERING OPTIONS
33+
################################################################################
34+
# This is the file used for server rendering of React when using `(prerender: true)`
35+
# If you are never using server rendering, you should set this to "".
36+
# Note, there is only one server bundle, unlike JavaScript where you want to minimize the size
37+
# of the JS sent to the client. For the server rendering, React on Rails creates a pool of
38+
# JavaScript execution instances which should handle any component requested.
39+
#
40+
# While you may configure this to be the same as your client bundle file, this file is typically
41+
# different. You should have ONE server bundle which can create all of your server rendered
42+
# React components.
43+
#
44+
config.server_bundle_js_file = "hello-world-bundle.js"
45+
end

0 commit comments

Comments
 (0)