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
69
The Complete Handbook to OpenTelemetry Metrics
prathamesh
1
52
Breaking down the Pillars of Observability: From data to outcomes
prathamesh
0
42
Monitoring vs. Debugging
prathamesh
0
100
Handling High Cardinality in Observability
prathamesh
1
71
Setting up Monitoring for Kubernetes
prathamesh
0
270
Monitoring vs. Debugging - SRE BLR Meetup
prathamesh
0
73
Monitoring vs. Debugging - IG Meetup 22nd July
prathamesh
2
88
Pune_User_Group.pdf
prathamesh
0
73
Other Decks in Programming
See All in Programming
Formの複雑さに立ち向かう
bmthd
1
720
ARA Ansible for the teams
kksat
0
150
iOSエンジニアから始める visionOS アプリ開発
nao_randd
3
120
2024年のkintone API振り返りと2025年 / kintone API look back in 2024
tasshi
0
210
ASP. NET CoreにおけるWebAPIの最新情報
tomokusaba
0
360
社内フレームワークとその依存性解決 / in-house framework and its dependency management
vvakame
1
550
技術を根付かせる / How to make technology take root
kubode
1
240
Ruby on cygwin 2025-02
fd0
0
140
Unity Android XR入門
sakutama_11
0
140
密集、ドキュメントのコロケーション with AWS Lambda
satoshi256kbyte
0
180
Flutter × Firebase Genkit で加速する生成 AI アプリ開発
coborinai
0
150
AWS Organizations で実現する、 マルチ AWS アカウントのルートユーザー管理からの脱却
atpons
0
130
Featured
See All Featured
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
31
2.1k
The Power of CSS Pseudo Elements
geoffreycrofte
75
5.5k
GraphQLの誤解/rethinking-graphql
sonatard
68
10k
Building Your Own Lightsaber
phodgson
104
6.2k
YesSQL, Process and Tooling at Scale
rocio
171
14k
How to train your dragon (web standard)
notwaldorf
90
5.8k
Optimising Largest Contentful Paint
csswizardry
34
3.1k
What's in a price? How to price your products and services
michaelherold
244
12k
Code Review Best Practice
trishagee
66
17k
Facilitating Awesome Meetings
lara
51
6.2k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.1k
Building Applications with DynamoDB
mza
93
6.2k
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