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
80
The Complete Handbook to OpenTelemetry Metrics
prathamesh
1
62
Breaking down the Pillars of Observability: From data to outcomes
prathamesh
0
52
Monitoring vs. Debugging
prathamesh
0
120
Handling High Cardinality in Observability
prathamesh
1
79
Setting up Monitoring for Kubernetes
prathamesh
0
280
Monitoring vs. Debugging - SRE BLR Meetup
prathamesh
0
78
Monitoring vs. Debugging - IG Meetup 22nd July
prathamesh
2
93
Pune_User_Group.pdf
prathamesh
0
87
Other Decks in Programming
See All in Programming
ワンバイナリWebサービスのススメ
mackee
10
7.4k
CQRS/ESのクラスとシステムフロー ~ RailsでフルスクラッチでCQRSESを組んで みたことから得た学び~
suzukimar
0
190
OpenNext + Hono on Cloudflare でイマドキWeb開発スタックを実現する
rokuosan
0
110
Investigating Multithreaded PostgreSQL
macdice
0
150
TypeScript エンジニアが Android 開発の世界に飛び込んだ話
yuisakamoto
6
950
テスト分析入門/Test Analysis Tutorial
goyoki
11
2.7k
Perlで痩せる
yuukis
1
660
MLOps Japan 勉強会 #52 - 特徴量を言語を越えて一貫して管理する, 『特徴量ドリブン』な MLOps の実現への試み
taniiicom
2
570
TVer iOSチームの共通認識の作り方 - Findy Job LT iOSアプリ開発の裏側 開発組織が向き合う課題とこれから
techtver
PRO
0
710
「MCPを使ってる人」が より詳しくなるための解説
yamaguchidesu
0
600
REST API設計の実践 – ベストプラクティスとその落とし穴
kentaroutakeda
2
310
Use Perl as Better Shell Script
karupanerura
0
650
Featured
See All Featured
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
6
660
A designer walks into a library…
pauljervisheath
205
24k
Large-scale JavaScript Application Architecture
addyosmani
512
110k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
137
34k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
42
2.3k
Art, The Web, and Tiny UX
lynnandtonic
298
21k
Six Lessons from altMBA
skipperchong
28
3.8k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
47
2.8k
Become a Pro
speakerdeck
PRO
28
5.4k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
14
1.5k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.6k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
45
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