Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Rethinking the view using React.js
Search
Prathamesh Sonpatki
June 04, 2015
Programming
3
2.7k
Rethinking the view using React.js
Talk given at RedDotRubyConf 2015
Prathamesh Sonpatki
June 04, 2015
Tweet
Share
More Decks by Prathamesh Sonpatki
See All by Prathamesh Sonpatki
Secrets to Monitor Kubernetes Workloads
prathamesh
1
84
The Complete Handbook to OpenTelemetry Metrics
prathamesh
1
64
Breaking down the Pillars of Observability: From data to outcomes
prathamesh
0
54
Monitoring vs. Debugging
prathamesh
0
120
Handling High Cardinality in Observability
prathamesh
1
82
Setting up Monitoring for Kubernetes
prathamesh
0
290
Monitoring vs. Debugging - SRE BLR Meetup
prathamesh
0
81
Monitoring vs. Debugging - IG Meetup 22nd July
prathamesh
2
97
Pune_User_Group.pdf
prathamesh
0
91
Other Decks in Programming
See All in Programming
第9回 情シス転職ミートアップ 株式会社IVRy(アイブリー)の紹介
ivry_presentationmaterials
1
230
エンジニア向け採用ピッチ資料
inusan
0
160
KotlinConf 2025 現地で感じたServer-Side Kotlin
n_takehata
1
230
GitHub Copilot and GitHub Codespaces Hands-on
ymd65536
1
120
AWS CDKの推しポイント 〜CloudFormationと比較してみた〜
akihisaikeda
3
310
既存デザインを変更せずにタップ領域を広げる方法
tahia910
1
240
PHPでWebSocketサーバーを実装しよう2025
kubotak
0
100
PHP 8.4の新機能「プロパティフック」から学ぶオブジェクト指向設計とリスコフの置換原則
kentaroutakeda
2
490
型付きアクターモデルがもたらす分散シミュレーションの未来
piyo7
0
810
FormFlow - Build Stunning Multistep Forms
yceruto
1
190
AIエージェントはこう育てる - GitHub Copilot Agentとチームの共進化サイクル
koboriakira
0
350
Blazing Fast UI Development with Compose Hot Reload (droidcon New York 2025)
zsmb
1
190
Featured
See All Featured
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
233
17k
Building Adaptive Systems
keathley
43
2.6k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
331
22k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
161
15k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
107
19k
Thoughts on Productivity
jonyablonski
69
4.7k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
667
120k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
GitHub's CSS Performance
jonrohan
1031
460k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.3k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
46
9.6k
Transcript
Rethinking the View using React Prathamesh Sonpatki @_cha1tanya
None
Thank You!
React
Build UI
V in MVC
Simple
if encodingCompleted removeLoader(); updateHeaderText(‘Encoding completed.’) else showLoader();
if encodingCompleted removeLoader(); updateHeaderText(‘Encoding completed.’) else showLoader(); if encodingCompleted <header>Encoding
Completed</header> else <div>Loading…</div>
Unconventional ideas
Components
var Hello = React.createClass({ render: function() { return ( <div>
Hello React World! </div> ); } }); React.render(<Hello />,document.body);
Component is fn()
None
<TweetCounts tweets=“3062” following=“509”/> <Tweets tweets={[..]}/> <Trends location=“pune" list={[..]} />
Reusable
var TimeLine = React.createClass({ render: function() { return ( <div>
<TweetCounts tweets=“3062” following=“509”/> <Tweets tweets={[..]}/> <Trends location=“pune" list={[..]} /> </div> ); } });
Composable
Separation of concerns
var Hello = React.createClass({ render: function() { return ( <div>
Hello React World! </div> ) } });
var Hello = React.createClass({ render: function() { return ( <div>
Hello React World! </div> ) } });
ಠ_ಠ
var Hello = React.createClass({ render: function() { return ( <div>
Hello React World! </div> ) } });
JSX
JSX
JSX
JSX is so bad, it even trolls Google!
None
None
var Hello = React.createClass({ render: function() { return ( <div>
Hello React World! </div> ) } });
JSX is like a healthy vegetable that tastes like decadent
chocolate cake. You feel guilty, but it’s good. Eric Elliot
JSX is like Durian Prathamesh Sonpatki
None
Give it 5 minutes https://signalvnoise.com/posts/3124- give-it-five-minutes
No templates
Any sufficiently complicated templating language contains an ad hoc, informally-specified,
bug-ridden, slow implementation of a turing complete programming language. - Jasim A Basheer
var tweets = [……] tweets.map(function(tweet) { return ( <Tweet author={tweet.author}
content={tweet.content} /> ); });
var tweets = [……] tweets.map(function(tweet) { return ( <Tweet author={tweet.author}
content={tweet.content} /> ); });
React === JavaScript #=> true
Managing Data
Data changing over time is root of evil Pete Hunt
Props
<Tweet> <div> <h1> Updated the website with a list of
the fringe events </h1> <p>@reddotrubyconf</p> </div> </Tweet>
Props come from above
<TweetsList> <Tweet author={tweet.author} content={tweet.content} /> <TweetsList/> <Tweet> <div> <h1> {this.props.author}
</h1> <p>@{this.props.content}</p> </div> </Tweet>
One way data flow
Immutable
propTypes
var Tweet = React.createClass({ propTypes: { author: React.PropTypes.string, content: React.PropTypes.string,
retweetCount: React.PropTypes.number, favoritesCount: React.PropTypes.number } });
State
State is internal
var Posts = React.createClass({ getInitialState: function() { return ( {
posts: [] } ); }, render: function() { var posts = this.state.posts.map(function(post) { return ( <Post author={post.author} content={post.content} /> ); }); } });
Avoid state as much as possible
props > state
Responding to state changes
var Posts = React.createClass({ loadPosts: function() { // fetch posts
}, componentDidMount: function() { this.loadPosts(); }, render: function() { var posts = this.state.posts.map(function(post) { return ( <Post author={post.author} content={post.content} /> ); }); } });
var Posts = React.createClass({ loadPosts: function() { // fetch posts
this.setState({posts: fetchedPosts}); }, componentDidMount: function() { this.loadPosts(); }, render: function() { this.loadPosts(); var posts = this.state.posts.map(function(post) { return ( <Post author={post.author} content={post.content} /> ); }); } });
Refresh
Re-Rendering the component
loadPosts: function() { // fetch posts this.setState({posts: fetchedPosts}); }, componentDidMount:
function() { this.loadPosts(); }, render: function() { var posts = this.state.posts.map(function(post) { return ( <Post author={post.author} content={post.content} /> ); }); } });
Re-Rendering the component tree
Re-Rendering
Isn’t it too slow?
Virtual DOM
var Hello = React.createClass({ render: function() { return ( <div>
Hello React World! </div> ) } });
var Hello = React.createClass({displayName: "Hello", render: function() { return (
React.createElement("div", null, "Hello React World!" ) ) } });
var Hello = React.createClass({displayName: "Hello", render: function() { return (
React.createElement("div", null, "Hello React World!" ) ) } });
Diff
Minimal set of changes
Batch updates
Nothing is new!
React❤Rails
gem “react-rails”
rails g react:install
//= require react //= require react_ujs //= require components app/assets/javascripts/components
app/assets/javascripts /component.js
JSX.to_js
<% @posts.each do |post| %> <tr> <td><%= post.author %></td> <td><%=
post.content %></td> </tr> <% end %>
None
<% @posts.each do |post| %> <%= react_component 'Post', { post:
post }, tag: 'tr' %> <% end %>
<% @posts.each do |post| %> <%= react_component 'Post', { post:
post }, tag: 'tr' %> <% end %>
<% @posts.each do |post| %> <%= react_component 'Post', { post:
post }, tag: 'tr' %> <% end %>
<% @posts.each do |post| %> <%= react_component 'Post', { post:
post }, tag: 'tr' %> <% end %>
<tr data-react-class="Post" data-react-props=“{‘post’:{'id':9, ‘author':'Prathamesh', 'content':'Getting ready for my talk’, ‘created_at':'2015-05-31T05:55:27.318Z',
'updated_at':'2015-05-31T05:55:27.318Z'}}" > </tr>
# /app/assets/javascripts/components/posts.js.jsx var Post = React.createClass({ render () { return
( <tr> <td> {this.props.post.author} </td> <td> {this.props.post.content} </td> </tr> ); } });
None
None
<%= react_component 'Posts', posts_url: posts_path %>
render: function() { setTimeout(this.loadPosts, 5000); var posts = this.state.posts.map(function(post) {
return ( <Post author={post.author} content={post.content} /> ); }); return ( <div> <h1> Posts </h1> <table className="table"> <thead> <tr> <th>Author</th> <th>Content</th> </tr> </thead> <tbody> {posts} </tbody> </table> </div> ); }
render: function() { setTimeout(this.loadPosts, 5000); var posts = this.state.posts.map(function(post) {
return ( <Post author={post.author} content={post.content} /> ); }); return ( <div> <h1> Posts </h1> <table className="table"> <thead> <tr> <th>Author</th> <th>Content</th> </tr> </thead> <tbody> {posts} </tbody> </table> </div> ); }
render: function() { setTimeout(this.loadPosts, 5000); var posts = this.state.posts.map(function(post) {
return ( <Post author={post.author} content={post.content} /> ); }); return ( <div> <h1> Posts </h1> <table className="table"> <thead> <tr> <th>Author</th> <th>Content</th> </tr> </thead> <tbody> {posts} </tbody> </table> </div> ); }
Syncs with asset pipeline
config.react.variant = :development config.react.variant = :production
Server side rendering
<% @posts.each do |post| %> <%= react_component 'Post', { post:
post }, { prerender: true, tag: ‘tr' } %> <% end %>
<% @posts.each do |post| %> <%= react_component 'Post', { post:
post }, { prerender: true, tag: ‘tr' } %> <% end %>
Caveats • No access to document • All the dependencies
must be specified in components.js • Components must be in global namespace
Component Generator
rails g react:component Tweet content:string fav_count:integer
var Tweet = React.createClass({ propTypes: { content: React.PropTypes.string, favCount: React.PropTypes.node
}, render: function() { return ( <div> <div>Content: {this.props.content}</div> <div>Fav Count: {this.props.fav_count}</div> </div> ); } });
React Native
A FRAMEWORK FOR BUILDING NATIVE APPS USING REACT
Learn once, write everywhere
Learning Curve
Just JavaScript
Just JavaScript & JSX
Just JavaScript
Easy to sneak into existing projects
Let me know your experience!
Thanks! @_cha1tanya @BigBinary React video series http://videos.bigbinary.com/categories/react http://videos.bigbinary.com/categories/tiny-reactjs-concepts