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
51
The Complete Handbook to OpenTelemetry Metrics
prathamesh
1
25
Breaking down the Pillars of Observability: From data to outcomes
prathamesh
0
20
Monitoring vs. Debugging
prathamesh
0
46
Handling High Cardinality in Observability
prathamesh
1
50
Setting up Monitoring for Kubernetes
prathamesh
0
230
Monitoring vs. Debugging - SRE BLR Meetup
prathamesh
0
52
Monitoring vs. Debugging - IG Meetup 22nd July
prathamesh
2
66
Pune_User_Group.pdf
prathamesh
0
69
Other Decks in Programming
See All in Programming
検証も兼ねて個人開発でHonoとかと向き合った話
hanetsuki
0
690
大規模Reactアプリのリアーキテクチャ~8万行のTanStack Query移行の軌跡~
kj455
4
940
二郎系ラーメンのコールで学ぶ AST 解析
memory1994
PRO
7
1.7k
DMMプラットフォームがTiDB Cloudを採用した背景
pospome
8
4k
Anthropic Cookbook のおすすめレシピ
schroneko
7
870
Elm Form Validation
bkuhlmann
0
510
OpenAPIを中心に考えるAPI開発入門 / Introduction to API Development with a Focus on OpenAPI
seike460
PRO
2
170
Prepare for Jakarta EE 11 - Performance and Developer Productivity
ivargrimstad
0
720
Designing for tomorrow's programming workflows
honnibal
PRO
2
120
try!Swift Tokyo 2024 参加報告 LT
akidon0000
1
220
Rubyでたのしむクリエイティブコーディング/Enjoy Creative coding with Ruby
chobishiba
1
180
⼤規模⾔語モデルの拡張(RAG)が 終わったかも知れない件について
nearme_tech
23
15k
Featured
See All Featured
Practical Orchestrator
shlominoach
182
9.7k
Side Projects
sachag
451
41k
Navigating Team Friction
lara
178
13k
How to name files
jennybc
65
93k
What's new in Ruby 2.0
geeforr
337
31k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
19
1.7k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
17
1.4k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
244
20k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
25
2.3k
Principles of Awesome APIs and How to Build Them.
keavy
121
16k
Scaling GitHub
holman
457
140k
A better future with KSS
kneath
231
16k
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