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
65
The Complete Handbook to OpenTelemetry Metrics
prathamesh
1
50
Breaking down the Pillars of Observability: From data to outcomes
prathamesh
0
40
Monitoring vs. Debugging
prathamesh
0
91
Handling High Cardinality in Observability
prathamesh
1
71
Setting up Monitoring for Kubernetes
prathamesh
0
260
Monitoring vs. Debugging - SRE BLR Meetup
prathamesh
0
72
Monitoring vs. Debugging - IG Meetup 22nd July
prathamesh
2
86
Pune_User_Group.pdf
prathamesh
0
73
Other Decks in Programming
See All in Programming
レガシーシステムにどう立ち向かうか 複雑さと理想と現実/vs-legacy
suzukihoge
14
2.2k
聞き手から登壇者へ: RubyKaigi2024 LTでの初挑戦が 教えてくれた、可能性の星
mikik0
1
130
弊社の「意識チョット低いアーキテクチャ」10選
texmeijin
5
24k
TypeScript Graph でコードレビューの心理的障壁を乗り越える
ysk8hori
2
1.1k
最新TCAキャッチアップ
0si43
0
140
Ethereum_.pdf
nekomatu
0
460
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
250
イベント駆動で成長して委員会
happymana
1
320
Flutterを言い訳にしない!アプリの使い心地改善テクニック5選🔥
kno3a87
1
170
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
210
受け取る人から提供する人になるということ
little_rubyist
0
230
Realtime API 入門
riofujimon
0
150
Featured
See All Featured
Building Applications with DynamoDB
mza
90
6.1k
Navigating Team Friction
lara
183
14k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
Practical Orchestrator
shlominoach
186
10k
Six Lessons from altMBA
skipperchong
27
3.5k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
A designer walks into a library…
pauljervisheath
204
24k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
120
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
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