{"id":1571,"date":"2017-04-14T21:08:23","date_gmt":"2017-04-14T12:08:23","guid":{"rendered":"http:\/\/lovelinux.mydns.jp\/?p=1571"},"modified":"2017-04-15T19:04:41","modified_gmt":"2017-04-15T10:04:41","slug":"react-router%e3%81%a8redux%e3%81%a8rails%e3%82%92%e7%b5%84%e3%81%bf%e5%90%88%e3%82%8f%e3%81%9b%e3%82%8b","status":"publish","type":"post","link":"https:\/\/lovelinux.mydns.jp\/?p=1571","title":{"rendered":"React-Router(react-router-dom)\u3068Redux\u3068Rails\u3092\u7d44\u307f\u5408\u308f\u305b\u308b"},"content":{"rendered":"<h3>\u306f\u3058\u3081\u306b<\/h3>\n<p>React+Redux+Rails\u3067SPA\u30a2\u30d7\u30ea\u3092\u4f5c\u308b\u3053\u3068\u306b\u306a\u3063\u305f\u306e\u3067\u3001\u3042\u308c\u3053\u308c\u8abf\u3079\u305f\u306e\u3067\u30e1\u30e2\u3067\u3059\u3002<br \/>\nSPA\u3068\u3044\u3046\u3068\u3001\u5f53\u7136\u3067\u3059\u304c\u3001\uff11\u3064\u306e\u30da\u30fc\u30b8\u3067\u3042\u308c\u3053\u308c\u3067\u304d\u3061\u3083\u3046\u3082\u306e\u3092\u4f5c\u3089\u306a\u304d\u3083\u3044\u3051\u306a\u3044\u308f\u3051\u3067\u30fb\u30fb\u30fb<br \/>\n\u305d\u3046\u306a\u308b\u3068\u30b3\u30f3\u30c6\u30f3\u30c4\u5236\u5fa1\u306e\u4ed5\u7d44\u307f\u304c\u5fc5\u8981\u3067\u3059\u3002\u305d\u3053\u3067\u767b\u5834\u3059\u308b\u306e\u304cReact-Router-Dom\u3002<br \/>\n\u3067\u3001\u3053\u308c\u5358\u4f53\u3067\u306f\u4f7f\u3044\u65b9\u7c21\u5358\u306a\u3093\u3067\u3059\u304c\u3001Rails\u3068Redux\u3092\u7d61\u3081\u3066\u52d5\u4f5c\u3055\u305b\u308b\u65b9\u6cd5\u304c\u5206\u304b\u3089\u306a\u304b\u3063\u305f\u306e\u3067<br \/>\n\u3042\u308c\u3053\u308c\u8abf\u3079\u3066\u3001\u306a\u3093\u3068\u304b\u52d5\u304f\u3068\u3053\u308d\u307e\u3067\u305f\u3069\u308a\u7740\u3044\u305f\u306e\u3067\u3001\u30e1\u30e2\u3092\u516c\u958b\u3057\u307e\u3059\u3002<br \/>\nReact-Router\u306f\u53e4\u3044\u3088\u3046\u3067\u3001React-Router-Dom\u306b\u3057\u3066\u307f\u307e\u3057\u305f\u3002\u3002<br \/>\n\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u304c\u8d85\u5c11\u306a\u304f\u3066\u307e\u3044\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>\u5b8c\u6210\u56f3\u306f\u3053\u3093\u306a\u611f\u3058\u3002<br \/>\n\u9752\u3044\u3068\u3053\u308d\u304c\u30e1\u30cb\u30e5\u30fc\u3067\u3001\u30af\u30ea\u30c3\u30af\u3059\u308b\u3068\u3001\u4e0b\u306e\u30b3\u30f3\u30c6\u30f3\u30c4\u304cReact-Router\u3067\u5165\u308c\u66ff\u308f\u308a\u307e\u3059\u3002<\/p>\n<p><a href=\"http:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2017\/04\/rr-2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2017\/04\/rr-2.png\" alt=\"rr-2\" width=\"650\" height=\"283\" class=\"alignnone size-full wp-image-1573\" srcset=\"https:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2017\/04\/rr-2.png 650w, https:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2017\/04\/rr-2-300x131.png 300w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/a><\/p>\n<p>\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306fGitHub\u3067\u516c\u958b\u3057\u3066\u3044\u307e\u3059\u3002\u5229\u7528\u65b9\u6cd5\u306f\u3001\u8a18\u4e8b\u306e\u672b\u5c3e\u3092\u53c2\u7167\u304f\u3060\u3055\u3044\u3002<\/p>\n<h3>Rails\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u4f5c\u6210<\/h3>\n<p>[bash]<br \/>\n$ rails new react_router_with_redux<\/p>\n<p>$ cd react_router_with_redux\/<br \/>\n[\/bash]<\/p>\n<h4>Gem\u3092\u8abf\u6574\u3057\u3066\u3001bundle install<\/h4>\n<p>[bash]<br \/>\n$ nano Gemfile<br \/>\n[\/bash]<\/p>\n<p>[bash]<br \/>\ngem &#8216;therubyracer&#8217;, platforms: :ruby<br \/>\ngem &#8216;react-rails&#8217;<br \/>\ngem &#8216;browserify-rails&#8217;<br \/>\n[\/bash]<\/p>\n<p>[bash]<br \/>\n$ bundle install<br \/>\n[\/bash]<\/p>\n<h3>application.rb\u3092\u7de8\u96c6<\/h3>\n<p>\u4f5c\u3063\u305f ES6\uff08JavaScript\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\uff09\u306e\u30bd\u30fc\u30b9\u3092ES5\u306b\u81ea\u52d5\u5909\u63db\u3059\u308b\u305f\u3081\u306e\u8a2d\u5b9a\u3092\u8ffd\u8a18<br \/>\nconfig.browserify_rails\u3000\u306e\u884c\u3092\u8ffd\u52a0<\/p>\n<p>config\/application.rb<\/p>\n<p>[bash]<br \/>\nrequire_relative &#8216;boot&#8217;<\/p>\n<p>require &#8216;rails\/all&#8217;<\/p>\n<p># Require the gems listed in Gemfile, including any gems<br \/>\n# you&#8217;ve limited to :test, :development, or :production.<br \/>\nBundler.require(*Rails.groups)<\/p>\n<p>module RailsRedux<br \/>\n  class Application < Rails::Application\n    # Settings in config\/environments\/* take precedence over those specified here.\n    # Application configuration should go into files in config\/initializers\n    # -- all .rb files in that directory are automatically loaded.\n+    config.browserify_rails.commandline_options = '-t babelify'\n  end\nend\n\n[\/bash]\n\n\n\n<h3>Rails \u306b React\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h3>\n<p>[bash]<br \/>\n$ rails g react:install<br \/>\n[\/bash]<\/p>\n<h3>application.js\u306e\u7de8\u96c6<\/h3>\n<p>app\/assets\/javascripts\/application.js<\/p>\n<p>\u30fbReact\u306f npm\u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u305f\u3082\u306e\u3092\u4f7f\u3046\u306e\u3067\u30ab\u30c3\u30c8\u3002<br \/>\n\u30fbrequire_tree . \u3082\u30ab\u30c3\u30c8<\/p>\n<p>\u5909\u66f4\u524d<\/p>\n<p>[bash]<br \/>\n\/\/= require jquery<br \/>\n\/\/= require jquery_ujs<br \/>\n\/\/= require turbolinks<br \/>\n\/\/= require react<br \/>\n\/\/= require react_ujs<br \/>\n\/\/= require components<br \/>\n\/\/= require_tree .<br \/>\n[\/bash]<\/p>\n<p>\u5909\u66f4\u5f8c<\/p>\n<p>[bash]<br \/>\n\/\/= require jquery<br \/>\n\/\/= require jquery_ujs<br \/>\n\/\/= require turbolinks<br \/>\n\/\/= require react_ujs<br \/>\n\/\/= require components<br \/>\n[\/bash]<\/p>\n<h3>React\u3001Redux\u3001etc\u2026\u306e\u6e96\u5099(node_modules\uff09<\/h3>\n<p>React\u3001Redux\u3001React-Router\u305d\u306e\u4ed6\u8af8\u3005\u3092 npm \u3092\u4f7f\u3063\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<\/p>\n<p>[bash]<br \/>\n$ npm init -y<br \/>\n$ npm install &#8211;save-dev browserify browserify-incremental babelify babel-preset-es2015 babel-preset-react<\/p>\n<p>$ npm install &#8211;save react react-dom react-redux redux redux-thunk react-router-dom<\/p>\n<p>[\/bash]<\/p>\n<h3>.babelrc \u30d5\u30a1\u30a4\u30eb\u3092Rails\u306e\u30eb\u30fc\u30c8\u30d5\u30a9\u30eb\u30c0\u306b\u4f5c\u6210<\/h3>\n<p>[bash]<br \/>\n{<br \/>\n  &#8220;presets&#8221;: [&#8220;es2015&#8221;, &#8220;react&#8221;]<br \/>\n}<br \/>\n[\/bash]<\/p>\n<h3>Redux\u306e\u6a19\u6e96\u7684\u306a\u30d5\u30a9\u30eb\u30c0\u3092\u4f5c\u6210<\/h3>\n<p>[bash]<br \/>\n$ cd app\/assets\/javascripts\/<br \/>\n$ mkdir actions containers reducers<br \/>\n[\/bash]<\/p>\n<h3>components.js\u7de8\u96c6<\/h3>\n<p>app\/assets\/javascripts\/components.js<\/p>\n<p>\u30a2\u30d7\u30ea\u540d\u306f \u300cMyApp\u300d\u3068\u3057\u307e\u3059\u3002<\/p>\n<p>myapp.js\u306f\u30a2\u30d7\u30ea\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u30dd\u30a4\u30f3\u30c8\u3068\u306a\u308bJS\u30d5\u30a1\u30a4\u30eb\u3067\u3059\u3002\u3053\u3053\u306b\u5b9a\u7fa9\u3057\u305f\u30af\u30e9\u30b9\u540d\uff08const\u5ba3\u8a00\uff09\u3092<br \/>\nwindow\u306e\u4e0b\u306b\u30bb\u30c3\u30c8\uff08\u5909\u6570\u3068\u3057\u3066\u516c\u958b\uff09\u3057\u3066\u3084\u308b\u3002<\/p>\n<p>\u5909\u66f4\u524d<\/p>\n<p>[bash]<br \/>\n\/\/= require_tree .\/components<br \/>\n[\/bash]<\/p>\n<p>\u5909\u66f4\u5f8c<\/p>\n<p>[bash]<br \/>\nwindow.React = require(&#8216;react&#8217;);<br \/>\nwindow.ReactDOM = require(&#8216;react-dom&#8217;);<br \/>\nwindow.MyApp = require(&#8216;.\/myapp.js&#8217;);<\/p>\n<p>[\/bash]<\/p>\n<h3>Redux\u3067\u30ab\u30a6\u30f3\u30bf\u30fc\u30a2\u30d7\u30ea\u3001\uff0b\u9759\u7684\u30da\u30fc\u30b8\u3092\u3072\u305f\u3059\u3089\u4f5c\u308b<\/h3>\n<p>\u3053\u3053\u4ee5\u964d\u3001\u3057\u3070\u3089\u304f\u306f\u3001Redux\u3067\u30ab\u30a6\u30f3\u30bf\u30fc\u306e\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\u3001\u30c7\u30af\u30ea\u30e1\u30f3\u30c8\u3067\u304d\u308b\u30a2\u30d7\u30ea\u3092\u4f5c\u308a\u307e\u3059\u3002<br \/>\n\u52a0\u3048\u3066\u3001\u9759\u7684\u30da\u30fc\u30b8\u3082\u3044\u304f\u3064\u304b\u4f5c\u308a\u307e\u3059\u3002<br \/>\n\u4f55\u3082\u8003\u3048\u305a\u306b\u30bd\u30fc\u30b9\u3092\u30b3\u30d4\u30fc\u3057\u3066\u3044\u3063\u3066\u3084\u3063\u3066\u304f\u3060\u3055\u3044\u30fb\u30fb\u30fb<\/p>\n<p>app\/assets\/javascripts\/actions\/index.js<br \/>\n[bash]<br \/>\nexport const counter_increment = () => {<br \/>\n  return {<br \/>\n    type: &#8220;INCREMENT&#8221;<br \/>\n  }<br \/>\n}<\/p>\n<p>export const counter_decrement = () => {<br \/>\n  return {<br \/>\n    type: &#8220;DECREMENT&#8221;<br \/>\n  }<br \/>\n}<\/p>\n<p>[\/bash]<\/p>\n<p>app\/assets\/javascripts\/components\/About.js<br \/>\n[bash]<br \/>\nimport React from &#8220;react&#8221;<\/p>\n<p>const About = () => {<br \/>\n  return (<\/p>\n<div>\n      This is About\n    <\/div>\n<p>  )<br \/>\n}<\/p>\n<p>export default About<\/p>\n<p>[\/bash]<\/p>\n<p>app\/assets\/javascripts\/components\/Contact.js<br \/>\n[bash]<br \/>\nimport React from &#8220;react&#8221;<\/p>\n<p>const Contact = () => {<br \/>\n  return (<\/p>\n<div>\n<h2>This is Contact Page<\/h2>\n<\/p><\/div>\n<p>  )<br \/>\n}<\/p>\n<p>export default Contact<\/p>\n<p>[\/bash]<\/p>\n<p>app\/assets\/javascripts\/components\/Counter.js<br \/>\n[bash]<br \/>\nimport React from &#8220;react&#8221;<\/p>\n<p>const Counter = ({counter, onIncrement, onDecrement}) => {<\/p>\n<p>  return (<\/p>\n<div>\n      <button onClick= {(e) => onIncrement()}><br \/>\n        Count Up<br \/>\n      <\/button><br \/>\n      <button onClick= {(e) => onDecrement()}><br \/>\n        Count Down<br \/>\n      <\/button><\/p>\n<h2>Counter = {counter}<\/h2>\n<\/p><\/div>\n<p>  )<br \/>\n}<\/p>\n<p>export default Counter<\/p>\n<p>[\/bash]<\/p>\n<p>app\/assets\/javascripts\/components\/Top.js<br \/>\n[bash]<br \/>\nimport React from &#8220;react&#8221;<\/p>\n<p>const Top = () => {<br \/>\n  return (<\/p>\n<div>\n<h2>This is Top Page<\/h2>\n<\/p><\/div>\n<p>  )<br \/>\n}<\/p>\n<p>export default Top<\/p>\n<p>[\/bash]<\/p>\n<p>\u3053\u3053 withRouter \u3068\u3044\u3046 react-router-dom \u306e\u30af\u30e9\u30b9\u3092\u4f7f\u3063\u3066\u307e\u3059\u3002<\/p>\n<p>app\/assets\/javascripts\/containers\/CCounter.js<br \/>\n[bash]<br \/>\nimport {connect} from &#8220;react-redux&#8221;<br \/>\nimport { withRouter } from &#8216;react-router-dom&#8217;<br \/>\nimport {counter_increment, counter_decrement} from &#8220;..\/actions\/index&#8221;<br \/>\nimport Counter from &#8220;..\/components\/Counter&#8221;<\/p>\n<p>const mapStateToProps = (state) => {<br \/>\n  return {<br \/>\n    counter: state.counter<br \/>\n  }<br \/>\n}<\/p>\n<p>const mapDispatchToProps = (dispatch) => {<br \/>\n  return {<br \/>\n    onIncrement: () => {<br \/>\n      dispatch(counter_increment())<br \/>\n    },<br \/>\n    onDecrement: () => {<br \/>\n      dispatch(counter_decrement())<br \/>\n    }<br \/>\n  }<br \/>\n}<\/p>\n<p>const CCounter = withRouter(connect(mapStateToProps, mapDispatchToProps)(Counter))<\/p>\n<p>export default CCounter<\/p>\n<p>[\/bash]<\/p>\n<p>app\/assets\/javascripts\/reducers\/counter.js<br \/>\n[bash]<br \/>\nconst counter = (state = 0, action) => {<br \/>\n  switch (action.type) {<br \/>\n    case &#8220;INCREMENT&#8221;:<br \/>\n      return state + 1<br \/>\n    case &#8220;DECREMENT&#8221;:<br \/>\n      return state &#8211; 1<br \/>\n    default:<br \/>\n      return state<br \/>\n  }<br \/>\n}<\/p>\n<p>export default counter<\/p>\n<p>[\/bash]<\/p>\n<p>app\/assets\/javascripts\/reducers\/root.js<br \/>\n[bash]<br \/>\nimport {combineReducers} from &#8220;redux&#8221;<br \/>\nimport counter from &#8220;.\/counter&#8221;<\/p>\n<p>const rootReducer = combineReducers({<br \/>\n  counter: counter<br \/>\n})<\/p>\n<p>export default rootReducer<\/p>\n<p>[\/bash]<\/p>\n<p>\u3053\u3053\u307e\u3067\u3067\u3001\u3068\u308a\u3042\u3048\u305a\u3001\u30ab\u30a6\u30f3\u30bf\u30fc\u30a2\u30d7\u30ea\u3068\u3001\u9759\u7684\u30da\u30fc\u30b8\u306f\u51fa\u6765\u4e0a\u304c\u308a\u3002<br \/>\n\u3053\u308c\u4ee5\u964d\u3001\u30a2\u30d7\u30ea\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u30dd\u30a4\u30f3\u30c8\u3001React-Router\u5468\u308a\u306b\u5fc5\u8981\u306a\u51e6\u7406\u3092\u8a18\u8f09\u3057\u307e\u3059\u3002<\/p>\n<h3>\u30a2\u30d7\u30ea\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u30dd\u30a4\u30f3\u30c8<\/h3>\n<p>app\/assets\/javascripts\/myapp.js<br \/>\n[bash]<br \/>\nimport React from &#8220;react&#8221;<br \/>\nimport {createStore} from &#8220;redux&#8221;<br \/>\nimport rootReducer from &#8220;.\/reducers\/root&#8221;<br \/>\nimport Root from &#8220;.\/components\/Root&#8221;<\/p>\n<p>class MyApp extends React.Component{<br \/>\n  render(){<br \/>\n    let store = createStore(rootReducer)<\/p>\n<p>    return (<br \/>\n      <Root store={store} \/><br \/>\n    )<br \/>\n  }<br \/>\n}<\/p>\n<p>export default MyApp<\/p>\n<p>[\/bash]<\/p>\n<h3>\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3Root<\/h3>\n<p>app\/assets\/javascripts\/components\/Root.js<br \/>\n[bash]<br \/>\nimport React from &#8220;react&#8221;<br \/>\nimport {Provider} from &#8220;react-redux&#8221;<br \/>\nimport { BrowserRouter as Router, Route} from &#8220;react-router-dom&#8221;<br \/>\nimport App from &#8220;.\/App&#8221;<br \/>\nimport Top from &#8220;.\/Top&#8221;<br \/>\nimport About from &#8220;.\/About&#8221;<br \/>\nimport Contact from &#8220;.\/Contact&#8221;<br \/>\nimport CCounter from &#8220;..\/containers\/CCounter&#8221;<\/p>\n<p>const Root = ({store}) => {<\/p>\n<p>  return (<br \/>\n    <Provider store={store}><br \/>\n      <Router><br \/>\n        <App><br \/>\n          <Route component={({match}) =><\/p>\n<div>\n              <Route path=\"\/myapp\/index\" component={Top} \/><br \/>\n              <Route path=\"\/myapp\/about\" component={About} \/><br \/>\n              <Route path=\"\/myapp\/contact\" component={Contact} \/><br \/>\n              <Route path=\"\/myapp\/counter\" component={CCounter} \/>\n            <\/div>\n<p>          }\/><br \/>\n        <\/App><br \/>\n      <\/Router><br \/>\n    <\/Provider><br \/>\n  )<br \/>\n}<\/p>\n<p>export default Root<\/p>\n<p>[\/bash]<\/p>\n<h3>\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30c8\u30c3\u30d7\u30da\u30fc\u30b8<\/h3>\n<p>app\/assets\/javascripts\/components\/App.js<br \/>\n[bash]<br \/>\nimport React from &#8220;react&#8221;<br \/>\nimport {NavLink} from &#8220;react-router-dom&#8221;<\/p>\n<p>class App extends React.Component {<\/p>\n<p>  render(){<br \/>\n    return (<\/p>\n<div className=\"app\">\n<h1>This is React Redux ReactRouter App<\/h1>\n<div>\n<ul className=\"menus\">\n<li><NavLink to=\"\/myapp\/index\" activeClassName=\"active\">Top<\/NavLink><\/li>\n<li><NavLink to=\"\/myapp\/counter\" activeClassName=\"active\">Counter<\/NavLink><\/li>\n<li><NavLink to=\"\/myapp\/about\" activeClassName=\"active\">About<\/NavLink><\/li>\n<li><NavLink to=\"\/myapp\/contact\" activeClassName=\"active\">Contact<\/NavLink><\/li>\n<\/ul><\/div>\n<div className=\"clear\">\n        <\/div>\n<p>        {this.props.children}\n      <\/p><\/div>\n<p>    )<br \/>\n  }<br \/>\n}<\/p>\n<p>export default App<\/p>\n<p>[\/bash]<\/p>\n<h3>Rails\u3000\u3067\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u3068\u30d3\u30e5\u30fc\u3092\u4f5c\u6210<\/h3>\n<p>[bash]<br \/>\n$ rails g controller myapp index<\/p>\n<p>[\/bash]<\/p>\n<h3>\u30d3\u30e5\u30fc\u3092\u8abf\u6574<\/h3>\n<p>\u30d3\u30e5\u30fc\u3092\u8abf\u6574\u3057\u3066\u3001SPA\u30a2\u30d7\u30ea\u306e\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3092\u51fa\u529b\u3057\u307e\u3059\u3002<\/p>\n<p>app\/views\/myapp\/index.html.erb<br \/>\n[bash]<br \/>\n<%= react_component(\"MyApp\") %><br \/>\n[\/bash]<\/p>\n<h3>\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u306e\u8abf\u6574<\/h3>\n<p>React-Router\u3067 \u30da\u30fc\u30b8\u79fb\u52d5\u3059\u308b\u305f\u3073\u306b myapp\/xxxx \u3078\u79fb\u52d5\u3057\u307e\u3059\u304c\u3001<br \/>\n\u3053\u306e\u30d1\u30b9\u3092\u3000myapp#index \u306b\u5272\u308a\u5f53\u3066\u3066\u3084\u308b\u3002<\/p>\n<p>config\/routes.rb<br \/>\n[bash]<br \/>\nRails.application.routes.draw do<br \/>\n  get &#8216;myapp\/index&#8217;<br \/>\n  get &#8216;myapp\/*path&#8217;, to: &#8216;myapp#index&#8217;<\/p>\n<p>  # For details on the DSL available within this file, see http:\/\/guides.rubyonrails.org\/routing.html<br \/>\nend<\/p>\n<p>[\/bash]<\/p>\n<h3>\u30c7\u30b6\u30a4\u30f3\u8abf\u6574<\/h3>\n<p>app\/assets\/stylesheets\/myapp.scss<br \/>\n[bash]<br \/>\n\/\/ Place all the styles related to the first controller here.<br \/>\n\/\/ They will automatically be included in application.css.<br \/>\n\/\/ You can use Sass (SCSS) here: http:\/\/sass-lang.com\/<\/p>\n<p>div.app{<br \/>\n  ul.menus{<br \/>\n    margin: 0;<br \/>\n    padding: 0;<\/p>\n<p>    li{<br \/>\n      float: left;<br \/>\n      list-style: none;<br \/>\n      width: 100px;<br \/>\n      background-color: #346299;<br \/>\n      padding: 15px;<br \/>\n      padding-top: 20px;<br \/>\n      border-right: 1px solid #cccccc;<\/p>\n<p>      a {<br \/>\n        color: #ffffff;<br \/>\n      }<\/p>\n<p>      a.active{<br \/>\n        color: #efe250;<br \/>\n      }<br \/>\n    }<br \/>\n  }<\/p>\n<p>  div.clear{<br \/>\n    clear: both;<br \/>\n  }<\/p>\n<p>[\/bash]<\/p>\n<h3>\u52d5\u4f5c\u78ba\u8a8d<\/h3>\n<p>[bash]<br \/>\n$ rails s<br \/>\n[\/bash]<\/p>\n<p><a href=\"http:\/\/localhost:3000\/myapp\/index\">http:\/\/localhost:3000\/myapp\/index<\/a><\/p>\n<p>\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30da\u30fc\u30b8\u304c\u8868\u793a\u3055\u308c\u308c\u3070\u3001\u6210\u529f\u3002<br \/>\n\u30ab\u30a6\u30f3\u30bf\u30fc\u306e\u30da\u30fc\u30b8\u3067\u3001\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3059\u308b\u3068\u3001\u30ab\u30a6\u30f3\u30bf\u30fc\u304c\u5897\u6e1b\u3057\u3066\u3044\u305f\u3089\u3001<br \/>\nRedux+React-Router\u306e\u7d44\u307f\u5408\u308f\u305b\u304c\u3046\u307e\u304f\u6a5f\u80fd\u3057\u3066\u3044\u308b\u3068\u3044\u3046\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><a href=\"http:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2017\/04\/rr-1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2017\/04\/rr-1.png\" alt=\"rr-1\" width=\"629\" height=\"255\" class=\"alignnone size-full wp-image-1572\" srcset=\"https:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2017\/04\/rr-1.png 629w, https:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2017\/04\/rr-1-300x122.png 300w\" sizes=\"auto, (max-width: 629px) 100vw, 629px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2017\/04\/rr-2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2017\/04\/rr-2.png\" alt=\"rr-2\" width=\"650\" height=\"283\" class=\"alignnone size-full wp-image-1573\" srcset=\"https:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2017\/04\/rr-2.png 650w, https:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2017\/04\/rr-2-300x131.png 300w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2017\/04\/rr-3.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2017\/04\/rr-3.png\" alt=\"rr-3\" width=\"629\" height=\"270\" class=\"alignnone size-full wp-image-1574\" srcset=\"https:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2017\/04\/rr-3.png 629w, https:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2017\/04\/rr-3-300x129.png 300w\" sizes=\"auto, (max-width: 629px) 100vw, 629px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2017\/04\/rr-4.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2017\/04\/rr-4.png\" alt=\"rr-4\" width=\"669\" height=\"282\" class=\"alignnone size-full wp-image-1575\" srcset=\"https:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2017\/04\/rr-4.png 669w, https:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2017\/04\/rr-4-300x126.png 300w\" sizes=\"auto, (max-width: 669px) 100vw, 669px\" \/><\/a><\/p>\n<h3>\u30d7\u30ed\u30b0\u30e9\u30e0\u304b\u3089\u30da\u30fc\u30b8(Route)\u3092\u5207\u308a\u66ff\u3048\u308b<\/h3>\n<p>\u30dc\u30bf\u30f3\u306e\u30af\u30ea\u30c3\u30af\u30a2\u30af\u30b7\u30e7\u30f3\u306a\u3069\u3067\u30da\u30fc\u30b8\u3092\u5207\u308a\u66ff\u3048\u305f\u3044\u5834\u5408\u304c\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u3002<br \/>\n\u6b63\u5f0f\u306a\u65b9\u6cd5\u304b\u308f\u304b\u308a\u307e\u305b\u3093\u304c\u3001\u4ee5\u4e0b\u306e\u65b9\u6cd5\u3067\u51fa\u6765\u307e\u3057\u305f\u3002<br \/>\nCounter\u30da\u30fc\u30b8\u304b\u3089\u3001Contact\u30da\u30fc\u30b8\u306b\u79fb\u52d5\u3057\u305f\u3089OK\u3067\u3059\u3002<\/p>\n<p>\u30d7\u30ed\u30d1\u30c6\u30a3\u306b history\u3068\u3044\u3046\u306e\u304c\u5b58\u5728\u3059\u308b\u307f\u305f\u3044\u3067\u3001\u305d\u308c\u3092\u5229\u7528\u3057\u307e\u3059\u3002<br \/>\napp\/assets\/javascripts\/components\/Counter.js<br \/>\n[bash]<br \/>\nimport React from &#8220;react&#8221;<\/p>\n<p>const Counter = ({counter, onIncrement, onDecrement, history}) => {<\/p>\n<p>  return (<\/p>\n<div>\n      <button onClick= {(e) => onIncrement()}><br \/>\n        Count Up<br \/>\n      <\/button><br \/>\n      <button onClick= {(e) => onDecrement()}><br \/>\n        Count Down<br \/>\n      <\/button><\/p>\n<h2>Counter = {counter}<\/h2>\n<p>      <button onClick= {(e) => history.push(&#8216;\/myapp\/contact&#8217;)}><br \/>\n        Go To Contact<br \/>\n      <\/button><\/p><\/div>\n<p>  )<br \/>\n}<\/p>\n<p>export default Counter<\/p>\n<p>[\/bash]<\/p>\n<p>\u30dc\u30bf\u30f3\u30af\u30ea\u30c3\u30af<br \/>\n<a href=\"http:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2017\/04\/rr-10.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2017\/04\/rr-10.png\" alt=\"rr-10\" width=\"599\" height=\"309\" class=\"alignnone size-full wp-image-1587\" srcset=\"https:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2017\/04\/rr-10.png 599w, https:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2017\/04\/rr-10-300x155.png 300w\" sizes=\"auto, (max-width: 599px) 100vw, 599px\" \/><\/a><\/p>\n<p>Contact\u30da\u30fc\u30b8\u30d8\u79fb\u52d5<br \/>\n<a href=\"http:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2017\/04\/rr-11.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2017\/04\/rr-11.png\" alt=\"rr-11\" width=\"636\" height=\"252\" class=\"alignnone size-large wp-image-1588\" srcset=\"https:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2017\/04\/rr-11.png 636w, https:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2017\/04\/rr-11-300x119.png 300w\" sizes=\"auto, (max-width: 636px) 100vw, 636px\" \/><\/a><\/p>\n<h3>GitHub\u30bd\u30fc\u30b9\u306e\u5229\u7528\u65b9\u6cd5<\/h3>\n<p>\u3053\u306e\u30bd\u30fc\u30b9\u306fGitHub\u3067\u516c\u958b\u3057\u3066\u3044\u307e\u3059\u306e\u3067\u3001\u3054\u81ea\u7531\u306b\u5229\u7528\u304f\u3060\u3055\u3044\u3002<\/p>\n<h4>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u624b\u9806<\/h4>\n<p>[bash]<br \/>\n$ git clone https:\/\/github.com\/h-mito\/react_router_with_redux<br \/>\n$ cd react_router_with_redux<br \/>\n$ bundle install<br \/>\n$ npm install<br \/>\n[\/bash]<\/p>\n<h4>\u52d5\u4f5c\u78ba\u8a8d<\/h4>\n<p>[bash]<br \/>\n$ rails s<br \/>\n[\/bash]<\/p>\n<p>\u30d6\u30e9\u30a6\u30b6\u30fc\u3067\u4ee5\u4e0b\u306eURL\u3078\u30a2\u30af\u30bb\u30b9<br \/>\n<a href=\"http:\/\/localhost:3000\/myapp\/index\">http:\/\/localhost:3000\/myapp\/index<\/a><\/p>\n<p>\u4ee5\u4e0a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u306f\u3058\u3081\u306b React+Redux+Rails\u3067SPA\u30a2\u30d7\u30ea\u3092\u4f5c\u308b\u3053\u3068\u306b\u306a\u3063\u305f\u306e\u3067\u3001\u3042\u308c\u3053\u308c\u8abf\u3079\u305f\u306e\u3067\u30e1\u30e2\u3067\u3059\u3002 SPA\u3068\u3044\u3046\u3068\u3001\u5f53\u7136\u3067\u3059\u304c\u3001\uff11\u3064\u306e\u30da\u30fc\u30b8\u3067\u3042\u308c\u3053\u308c\u3067\u304d\u3061\u3083\u3046\u3082\u306e\u3092\u4f5c\u3089\u306a\u304d\u3083\u3044\u3051\u306a\u3044\u308f\u3051\u3067\u30fb\u30fb\u30fb \u305d\u3046\u306a\u308b\u3068\u30b3\u30f3\u30c6\u30f3\u30c4\u5236\u5fa1\u306e\u4ed5\u7d44\u307f\u304c\u5fc5\u8981\u3067\u3059\u3002\u305d\u3053\u3067\u767b\u5834\u3059\u308b\u306e\u304cReact-Router-Dom\u3002 \u3067\u3001\u3053\u308c\u5358\u4f53\u3067\u306f\u4f7f\u3044\u65b9\u7c21\u5358\u306a\u3093\u3067\u3059\u304c\u3001Rails\u3068Redux\u3092\u7d61\u3081\u3066\u52d5\u4f5c\u3055\u305b\u308b\u65b9\u6cd5\u304c\u5206\u304b\u3089\u306a\u304b\u3063\u305f\u306e\u3067 \u3042\u308c\u3053\u308c\u8abf\u3079\u3066\u3001\u306a\u3093\u3068\u304b\u52d5\u304f\u3068\u3053\u308d\u307e\u3067\u305f\u3069\u308a\u7740\u3044\u305f\u306e\u3067\u3001\u30e1\u30e2\u3092\u516c\u958b\u3057\u307e\u3059\u3002 React-Router\u306f\u53e4\u3044\u3088\u3046\u3067\u3001React-Router-Dom\u306b\u3057\u3066\u307f\u307e\u3057\u305f\u3002\u3002 \u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u304c\u8d85\u5c11\u306a\u304f\u3066\u307e\u3044\u308a\u307e\u3057\u305f\u3002 \u5b8c\u6210\u56f3\u306f\u3053\u3093\u306a\u611f\u3058\u3002 \u9752\u3044\u3068\u3053\u308d\u304c\u30e1\u30cb\u30e5\u30fc\u3067\u3001\u30af\u30ea\u30c3\u30af\u3059\u308b\u3068\u3001\u4e0b\u306e\u30b3\u30f3\u30c6\u30f3\u30c4\u304cReact-Router\u3067\u5165\u308c\u66ff\u308f\u308a\u307e\u3059\u3002 \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306fGitHub\u3067\u516c\u958b\u3057\u3066\u3044\u307e\u3059\u3002\u5229\u7528\u65b9\u6cd5\u306f\u3001\u8a18\u4e8b\u306e\u672b\u5c3e\u3092\u53c2\u7167\u304f\u3060\u3055\u3044\u3002 Rails\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u4f5c\u6210 [bash] $ rails new react_router_with_redux $ cd react_router_with_redux\/ [\/bash] Gem\u3092\u8abf\u6574\u3057\u3066\u3001bundle install [bash] $ nano Gemfile [\/bash] [bash] gem &#8216;therubyracer&#8217;, platforms: :ruby gem &#8216;react-rails&#8217; gem &#8216;browserify-rails&#8217; [\/bash] [bash] $ bundle install [\/bash] application.rb\u3092\u7de8\u96c6 \u4f5c\u3063\u305f ES6\uff08JavaScript\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\uff09\u306e\u30bd\u30fc\u30b9\u3092ES5\u306b\u81ea\u52d5\u5909\u63db\u3059\u308b\u305f\u3081\u306e\u8a2d\u5b9a\u3092\u8ffd\u8a18 config.browserify_rails\u3000\u306e\u884c\u3092\u8ffd\u52a0 config\/application.rb [bash] require_relative &#8216;boot&#8217; require &#8216;rails\/all&#8217; # Require the gems listed in Gemfile, including any gems # you&#8217;ve limited to :test, :development, or :production. Bundler.require(*Rails.groups) module RailsRedux class Application < Rails::Application # Settings in config\/environments\/* take precedence over those specified here. # Application configuration should go into files in config\/initializers # -- all .rb files in that directory are automatically loaded. + config.browserify_rails.commandline_options = '-t babelify' end end [\/bash] Rails \u306b React\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb [bash] $ rails g react:install [\/bash] application.js\u306e\u7de8\u96c6 app\/assets\/javascripts\/application.js \u30fbReact\u306f npm\u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u305f\u3082\u306e\u3092\u4f7f\u3046\u306e\u3067\u30ab\u30c3\u30c8\u3002 \u30fbrequire_tree . \u3082\u30ab\u30c3\u30c8 \u5909\u66f4\u524d [bash] \/\/= require jquery \/\/= require jquery_ujs \/\/= require turbolinks \/\/= require react \/\/= require react_ujs \/\/= require components \/\/= require_tree . [\/bash] \u5909\u66f4\u5f8c [bash] \/\/= require jquery \/\/= require jquery_ujs \/\/= require turbolinks \/\/= require react_ujs \/\/= require components [\/bash] React\u3001Redux\u3001etc\u2026\u306e\u6e96\u5099(node_modules\uff09 React\u3001Redux\u3001React-Router\u305d\u306e\u4ed6\u8af8\u3005\u3092 npm \u3092\u4f7f\u3063\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002 [bash] $ npm init -y $ npm install &#8211;save-dev browserify browserify-incremental babelify babel-preset-es2015 babel-preset-react $ npm install &#8211;save react react-dom react-redux redux redux-thunk react-router-dom [\/bash] .babelrc \u30d5\u30a1\u30a4\u30eb\u3092Rails\u306e\u30eb\u30fc\u30c8\u30d5\u30a9\u30eb\u30c0\u306b\u4f5c\u6210 [bash] { &#8220;presets&#8221;: [&#8220;es2015&#8221;, &#8220;react&#8221;] } [\/bash] Redux\u306e\u6a19\u6e96\u7684\u306a\u30d5\u30a9\u30eb\u30c0\u3092\u4f5c\u6210 [bash] $ cd app\/assets\/javascripts\/ $ mkdir actions containers reducers [\/bash] components.js\u7de8\u96c6 app\/assets\/javascripts\/components.js \u30a2\u30d7\u30ea\u540d\u306f \u300cMyApp\u300d\u3068\u3057\u307e\u3059\u3002 myapp.js\u306f\u30a2\u30d7\u30ea\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u30dd\u30a4\u30f3\u30c8\u3068\u306a\u308bJS\u30d5\u30a1\u30a4\u30eb\u3067\u3059\u3002\u3053\u3053\u306b\u5b9a\u7fa9\u3057\u305f\u30af\u30e9\u30b9\u540d\uff08const\u5ba3\u8a00\uff09\u3092 window\u306e\u4e0b\u306b\u30bb\u30c3\u30c8\uff08\u5909\u6570\u3068\u3057\u3066\u516c\u958b\uff09\u3057\u3066\u3084\u308b\u3002 \u5909\u66f4\u524d [bash] \/\/= require_tree .\/components [\/bash] \u5909\u66f4\u5f8c [bash] window.React = require(&#8216;react&#8217;); window.ReactDOM = require(&#8216;react-dom&#8217;); window.MyApp = require(&#8216;.\/myapp.js&#8217;); [\/bash] Redux\u3067\u30ab\u30a6\u30f3\u30bf\u30fc\u30a2\u30d7\u30ea\u3001\uff0b\u9759\u7684\u30da\u30fc\u30b8\u3092\u3072\u305f\u3059\u3089\u4f5c\u308b \u3053\u3053\u4ee5\u964d\u3001\u3057\u3070\u3089\u304f\u306f\u3001Redux\u3067\u30ab\u30a6\u30f3\u30bf\u30fc\u306e\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\u3001\u30c7\u30af\u30ea\u30e1\u30f3\u30c8\u3067\u304d\u308b\u30a2\u30d7\u30ea\u3092\u4f5c\u308a\u307e\u3059\u3002 \u52a0\u3048\u3066\u3001\u9759\u7684\u30da\u30fc\u30b8\u3082\u3044\u304f\u3064\u304b\u4f5c\u308a\u307e\u3059\u3002 \u4f55\u3082\u8003\u3048\u305a\u306b\u30bd\u30fc\u30b9\u3092\u30b3\u30d4\u30fc\u3057\u3066\u3044\u3063\u3066\u3084\u3063\u3066\u304f\u3060\u3055\u3044\u30fb\u30fb\u30fb app\/assets\/javascripts\/actions\/index.js [bash] export const counter_increment = () => { return { type: &#8220;INCREMENT&#8221; } } export const counter_decrement = () => { return { type: &#8220;DECREMENT&#8221; } } [\/bash] app\/assets\/javascripts\/components\/About.js [bash] import React from &#8220;react&#8221; const About = () => { return ( This is About ) } export&#8230;<\/p>\n","protected":false},"author":1,"featured_media":1579,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[6,77],"tags":[71,72,76],"class_list":{"0":"post-1571","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-rails","8":"category-react","9":"tag-rails","10":"tag-reactjs","11":"tag-redux"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=\/wp\/v2\/posts\/1571","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1571"}],"version-history":[{"count":12,"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=\/wp\/v2\/posts\/1571\/revisions"}],"predecessor-version":[{"id":1586,"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=\/wp\/v2\/posts\/1571\/revisions\/1586"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=\/wp\/v2\/media\/1579"}],"wp:attachment":[{"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}