react_rails

13
react_rails Ruby舞鶴2015.8.23

Transcript of react_rails

Page 1: react_rails

react_railsRuby舞鶴2015.8.23

Page 2: react_rails

自己紹介

近藤 茂(twitter: @subcigel, github: cncgl)

フリーエンジニア

主にバックエンドとフロントエンド

10年ぐらいのブランクがある(ほぼ新人)

Page 3: react_rails

Reactとは

Facebook製JavaScript UI ライブラリ

単一方向データフロー

Virtual DOM

JSXシンタックス

Page 4: react_rails

react_railsとは

Rails で React を扱いやすくする

Page 5: react_rails

元記事

Qiita: “react-railsを使ってReactのTutorialを

やってみる” @joe-re

Page 6: react_rails

使い方(Rails)

$ rails new react-rails-sample —bundle-skip

$ cd react-rails-sample

$ vim Gemfilegem ‘react_rails’, ‘~>1.2.0’

$ bundle install —path vendor/bundle

Page 7: react_rails

使い方(React)$ rails g react:install

create app/assets/javascripts/components create app/assets/javascripts/components/.gitkeepinsert app/assets/javascripts/application.js insert app/assets/javascripts/application.js insert app/assets/javascripts/application.js create app/assets/javascripts/components.js

Page 8: react_rails

React Component 生成

$rails g react:component CommentBox create app/assets/javascripts/components/comment_box.js.jsx

Page 9: react_rails

JSX 修正$ vim app/assets/javascripts/components/comment_box.js.jsx

var CommentBox = React.createClass({ render: function() { return ( <div className="commentBox"> Hello, world! I am a CommentBox. </div> ); }});

Page 10: react_rails

controller & view

$ rails g controller comments

$ vim app/views/comments/index.html.erb<%= react_component(‘CommentBox’) %>

Page 11: react_rails

Routing

$ vim config/routes.rb root ‘comments#index’localhost:3000 でアクセスして表示されれば

成功

Page 12: react_rails

Server Rendering

index.html.erb で使用した react_component()

において { prerender: true } を渡すことにより

server 側で rendering できるようになる(少

し、条件がある)。

Page 13: react_rails

まとめ

Rails で React するなら react_rails が便利

react_rails にはサーバーサイドレンダリング

の機能があり、SEOに優れている