Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic, non-redux, install #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,5 @@ end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

gem 'mini_racer', platforms: :ruby
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ GEM
jbuilder (2.7.0)
activesupport (>= 4.2.0)
multi_json (>= 1.2)
libv8 (5.9.211.38.1-x86_64-darwin-16)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand All @@ -95,6 +96,8 @@ GEM
mime-types-data (3.2016.0521)
mini_mime (0.1.4)
mini_portile2 (2.2.0)
mini_racer (0.1.14)
libv8 (~> 5.9)
minitest (5.10.3)
multi_json (1.12.1)
nio4r (2.1.0)
Expand Down Expand Up @@ -199,6 +202,7 @@ DEPENDENCIES
coffee-rails (~> 4.2)
jbuilder (~> 2.5)
listen (>= 3.0.5, < 3.2)
mini_racer
puma (~> 3.7)
rails (~> 5.1.3)
react_on_rails!
Expand Down
4 changes: 4 additions & 0 deletions Procfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
web: rails s -p 3000

# Next line runs a watch process with webpack
client: sh -c 'rm -rf public/packs/* || true && bundle exec rake react_on_rails:locale && bin/webpack -w'
5 changes: 5 additions & 0 deletions Procfile.dev-server
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
web: rails s -p 3000

# Next line runs the webpack-dev-server
# You can edit config/webpacker.yml to set HMR to true to see hot reloading
client: sh -c 'rm -rf public/packs/* || true && bundle exec rake react_on_rails:locale && bin/webpack-dev-server'
9 changes: 9 additions & 0 deletions app/controllers/hello_world_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class HelloWorldController < ApplicationController
layout "hello_world"

def index
@hello_world_props = { name: "Stranger" }
end
end
45 changes: 45 additions & 0 deletions app/javascript/bundles/HelloWorld/components/HelloWorld.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import PropTypes from 'prop-types';
import React from 'react';

export default class HelloWorld extends React.Component {
static propTypes = {
name: PropTypes.string.isRequired, // this is passed from the Rails view
};

/**
* @param props - Comes from your rails view.
*/
constructor(props) {
super(props);

// How to set initial state in ES6 class syntax
// https://facebook.github.io/react/docs/reusable-components.html#es6-classes
this.state = { name: this.props.name };
}

updateName = (name) => {
this.setState({ name });
};

render() {
return (
<div>
<h3>
Hello, {this.state.name}!
</h3>
<hr />
<form >
<label htmlFor="name">
Say hello to:
</label>
<input
id="name"
type="text"
value={this.state.name}
onChange={(e) => this.updateName(e.target.value)}
/>
</form>
</div>
);
}
}
8 changes: 8 additions & 0 deletions app/javascript/packs/hello-world-bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import ReactOnRails from 'react-on-rails';

import HelloWorld from '../bundles/HelloWorld/components/HelloWorld';

// This is how react_on_rails can see the HelloWorld in the browser.
ReactOnRails.register({
HelloWorld,
});
2 changes: 2 additions & 0 deletions app/views/hello_world/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Hello World</h1>
<%= react_component("HelloWorld", props: @hello_world_props, prerender: false) %>
12 changes: 12 additions & 0 deletions app/views/layouts/hello_world.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>ReactOnRailsWithWebpacker</title>
<%= csrf_meta_tags %>
<%= javascript_pack_tag 'hello-world-bundle' %>
</head>

<body>
<%= yield %>
</body>
</html>
41 changes: 41 additions & 0 deletions config/initializers/react_on_rails.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

# See docs/basics/configuration.md for many more options

ReactOnRails.configure do |config|
# This configures the script to run to build the production assets by webpack. Set this to nil
# if you don't want react_on_rails building this file for you.
config.npm_build_production_command = "RAILS_ENV=production bin/webpack"

################################################################################
################################################################################
# TEST CONFIGURATION OPTIONS
# Below options are used with the use of this test helper:
# ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
################################################################################

# If you are using this in your spec_helper.rb (or rails_helper.rb):
#
# ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
#
# with rspec then this controls what yarn command is run
# to automatically refresh your webpack assets on every test run.
#
config.build_test_command = "RAILS_ENV=test bin/webpack"

################################################################################
################################################################################
# SERVER RENDERING OPTIONS
################################################################################
# This is the file used for server rendering of React when using `(prerender: true)`
# If you are never using server rendering, you should set this to "".
# Note, there is only one server bundle, unlike JavaScript where you want to minimize the size
# of the JS sent to the client. For the server rendering, React on Rails creates a pool of
# JavaScript execution instances which should handle any component requested.
#
# While you may configure this to be the same as your client bundle file, this file is typically
# different. You should have ONE server bundle which can create all of your server rendered
# React components.
#
config.server_bundle_js_file = "hello-world-bundle.js"
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Rails.application.routes.draw do
get 'hello_world', to: 'hello_world#index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"babel-preset-react": "^6.24.1",
"prop-types": "^15.5.10",
"react": "^15.6.1",
"react-dom": "^15.6.1"
"react-dom": "^15.6.1",
"react-on-rails": "^9.0.0-beta.11"
},
"devDependencies": {
"webpack-dev-server": "^2.7.1"
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4172,6 +4172,10 @@ react-dom@^15.6.1:
object-assign "^4.1.0"
prop-types "^15.5.10"

react-on-rails@^9.0.0-beta.11:
version "9.0.0-beta.11"
resolved "https://registry.yarnpkg.com/react-on-rails/-/react-on-rails-9.0.0-beta.11.tgz#f6951129d7c2a67479281ca900859d5ac0aab978"

react@^15.6.1:
version "15.6.1"
resolved "https://registry.yarnpkg.com/react/-/react-15.6.1.tgz#baa8434ec6780bde997cdc380b79cd33b96393df"
Expand Down