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
ReactJS - The Container component pattern
Search
Khánh Trần
October 25, 2016
Programming
0
61
ReactJS - The Container component pattern
A short slide for ReactJS workshop.
Khánh Trần
October 25, 2016
Tweet
Share
More Decks by Khánh Trần
See All by Khánh Trần
Docker-Compose workshop
rualatngua
2
230
Seneca workshop
rualatngua
0
140
[ReactJS How to Workshop] Day 01
rualatngua
1
53
Other Decks in Programming
See All in Programming
Using AI Tools Around Software Development
inouehi
0
1.2k
SODA - FACT BOOK
sodainc
1
990
既存デザインを変更せずにタップ領域を広げる方法
tahia910
1
230
F#で自在につくる静的ブログサイト - 関数型まつり2025
pizzacat83
0
300
UPDATEがシステムを複雑にする? イミュータブルデータモデルのすすめ
shimomura
1
550
Webからモバイルへ Vue.js × Capacitor 活用事例
naokihaba
0
700
イベントストーミングから始めるドメイン駆動設計
jgeem
4
850
LINEヤフー データグループ紹介
lycorp_recruit_jp
0
710
Create a website using Spatial Web
akkeylab
0
290
Beyond Portability: Live Migration for Evolving WebAssembly Workloads
chikuwait
0
380
Enterprise Web App. Development (2): Version Control Tool Training Ver. 5.1
knakagawa
1
110
Go1.25からのGOMAXPROCS
kuro_kurorrr
1
740
Featured
See All Featured
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
10
910
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
52
2.8k
Building Flexible Design Systems
yeseniaperezcruz
328
39k
Git: the NoSQL Database
bkeepers
PRO
430
65k
Embracing the Ebb and Flow
colly
86
4.7k
The Straight Up "How To Draw Better" Workshop
denniskardys
233
140k
The Language of Interfaces
destraynor
158
25k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
The Illustrated Children's Guide to Kubernetes
chrisshort
48
50k
KATA
mclloyd
29
14k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.3k
Transcript
The Container Component pattern
Container Components? A container does data fetching and then renders
its corresponding sub-component. That’s it. — - Jason Bonta • StockWidgetContainer => StockWidget • TagCloudContainer => TagCloud
Why containers? // CommentList.js class CommentList extends React.Component { constructor()
{ super(); this.state = { comments: [] } } componentDidMount() { $.ajax({ url: "/my-comments.json", dataType: 'json', success: function(comments) { this.setState({comments: comments}); }.bind(this) }); } render() { return <ul> {this.state.comments.map(renderComment)} </ul>; } renderComment({body, author}) { return <li>{body}—{author}</li>; } }
There’s nothing “wrong” with this but... ...you miss out on
a few benefits of React
Reusability CommentList can’t be reused unless under the exact same
circumstances. • Top Customer Reviews vs. Most Recent Customer Reviews
Data structure Your markup components should state expectations of the
data they require. PropTypes are great for this. — - Learn React with chantastic
This time with a container // CommentListContainer.js class CommentListContainer extends
React.Component { constructor() { super(); this.state = { comments: [] } } componentDidMount() { $.ajax({ url: "/my-comments.json", dataType: 'json', success: function(comments) { this.setState({comments: comments}); }.bind(this) }); } render() { return <CommentList comments={this.state.comments} />; } }
Let's rework on CommentList to get comments as a prop
// CommentList.js class CommentList extends React.Component { constructor(props) { super(props); } render() { return <ul> {this.props.comments.map(renderComment)} </ul>; } renderComment({body, author}) { return <li>{body}—{author}</li>; } }
So, what did we get? • We’ve separated our data-fetching
and rendering concerns. • We’ve made our CommentList component reusable. • ...next >
...we’ve given CommentList the ability to set PropTypes and fail
loudly CommentList.propTypes = { comments: React.PropTypes.object.isRequired }
Q&A
Bonus: Making your app fast with high- performance components