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
62
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
16年目のピクシブ百科事典を支える最新の技術基盤 / The Modern Tech Stack Powering Pixiv Encyclopedia in its 16th Year
ahuglajbclajep
5
990
AI時代のキャリアプラン「技術の引力」からの脱出と「問い」へのいざない / tech-gravity
minodriven
20
6.7k
AIで開発はどれくらい加速したのか?AIエージェントによるコード生成を、現場の評価と研究開発の評価の両面からdeep diveしてみる
daisuketakeda
1
970
Architectural Extensions
denyspoltorak
0
270
AIと一緒にレガシーに向き合ってみた
nyafunta9858
0
160
AIエージェントの設計で注意するべきポイント6選
har1101
7
3.4k
余白を設計しフロントエンド開発を 加速させる
tsukuha
7
2.1k
Smart Handoff/Pickup ガイド - Claude Code セッション管理
yukiigarashi
0
120
Spinner 軸ズレ現象を調べたらレンダリング深淵に飲まれた #レバテックMeetup
bengo4com
1
230
Implementation Patterns
denyspoltorak
0
280
インターン生でもAuth0で認証基盤刷新が出来るのか
taku271
0
190
組織で育むオブザーバビリティ
ryota_hnk
0
170
Featured
See All Featured
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
120
The Power of CSS Pseudo Elements
geoffreycrofte
80
6.1k
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
80
A Modern Web Designer's Workflow
chriscoyier
698
190k
Optimizing for Happiness
mojombo
379
71k
Visualization
eitanlees
150
17k
Mobile First: as difficult as doing things right
swwweet
225
10k
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.3k
Color Theory Basics | Prateek | Gurzu
gurzu
0
190
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
62
Side Projects
sachag
455
43k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.1k
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