(翻译) Container Components
这篇文章翻译自Medium的一篇文章:Container Components
选择这篇文章翻译的原因是,在刚接触React的时候,这篇文章很好的指引我了解Container Components模式。
Container Component模式
Container components模式是一款很棒的React模式,对我的代码影响很大。
Jason Bonta在React.js大会中说过他们如何在Facebook开发高效的组建。在这个演讲中,他提到了container components模式。
其实原理很简单:
一个container负责数据的获取,然后渲染它对应的下级component。就这些而已。
“对应的”的意思是他们拥有共同的名称:
StockWidgetContainer => StockWidget
TagCloudContainer => TagCloud
PartyPooperListContainer => PartyPooperList
大概就是这个意思。
为什么要用Containers呢?
假设我们需要做一个展示评论的组建。在你不知道container components模式之前,你会把所有的东西都放在一个里面:
// 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>;
}
}
你的这个组建要同时负责获取数据和展示数据。当然,这种做法没有什么错的,但是你没有很好的利用React的一些优势。
复用性
除非在一个一模一样的使用环境下,你无法重用CommentList组建。
数据结构
你的展示组建对需要的数据架构有具体的要求,而PropTypes能够很好地满足这个要求。
展示组建对数据结构有一定的要求,但是却没有办法限制数据类型。如果传入的json结构发生了改变,那么组建就会down掉,并不会抛出任何错误。
如果我们使用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} />;
}
}
同时,我们修改一下CommentList让它可以接受一个comments的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>;
}
}
所以,我们这么做获得了什么?
我们获取了很多东西…
我们分离了数据获取和数据渲染的逻辑。
我们让CommentList变成了可复用的组建。
我们允许CommentList可以通过PropTypes来限制props数据个格式。如果props格式出错,就会报错。
我是container components模式的忠实簇拥者,它帮助我更好的完成React项目。大家不妨试一试,也可以观看这个视屏。一个很棒的模式。
再次声明:这篇文章翻译自Medium的一篇文章:Container Components
如果要转载,请至少著名上面的文章出处,谢谢。
(翻译) Container Components的更多相关文章
- (翻译)React Container Components
原文:Container Components Container Components 在 React 模式上对我的代码有最深远影响的一个模式叫 container component 模式. 在 ...
- [Redux] Extracting Container Components (FilterLink)
Learn how to avoid the boilerplate of passing the props down the intermediate components by introduc ...
- Presentational and Container Components
https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0 There’s a simple pattern I fi ...
- [Redux] Extracting Container Components -- Complete
Clean TodoApp Component, it doesn't need to receive any props from the top level component: const To ...
- [Redux] Extracting Container Components -- VisibleTodoList
Code to be refacted: const TodoList = ({ todos, onTodoClick }) => ( <ul> {todos.map(todo =& ...
- [Redux] Redux: Extracting Container Components -- AddTodo
Code to be refactored: const AddTodo = ({ onAddClick }) => { let input; return ( <div> < ...
- 关于React的Container&Presentational Component模型结构分析
react.js javascript 3 之前翻译了两篇关于Container&Presentational Component模型的文章,一篇是基础的Container和Component ...
- [Angular 2] Passing Observables into Components with Async Pipe
The components inside of your container components can easily accept Observables. You simply define ...
- C#委托,事件理解入门 (译稿)
原文地址:http://www.codeproject.com/Articles/4773/Events-and-Delegates-Simplified 引用翻译地址:http://www.cnbl ...
随机推荐
- JS与jquery书写插件规范
什么是封装呢? 我的理解就是 把一个功能单独做成一个组件,就像做饺子,以前做饺子必须自己先用面粉做饺子皮,再做饺子馅,然后再手工包饺子,但是现在人们发明了自动包饺子机器,虽然机器里面的每一步骤和你自己 ...
- 用python获取服务器硬件信息[转]
#!/usr/bin/env python # -*- coding: utf-8 -*- import rlcompleter, readline readline.parse_and_bind(' ...
- 2014年国内最热门的.NET开源平台
http://developer.51cto.com/art/201501/464292.htm
- Git Base 操作(一)
Git常用命令 1. 命令git init把这个目录变成Git可以管理的仓库: 2. 命令git commit把文件提交到仓库 这里需要注意的是,Git只能跟踪文本文件的改动,如txt文件,网页,所有 ...
- Codeforces 371B Fox Dividing Cheese(简单数论)
题目链接 Fox Dividing Cheese 思路:求出两个数a和b的最大公约数g,然后求出a/g,b/g,分别记为c和d. 然后考虑c和d,若c或d中存在不为2,3,5的质因子,则直接输出-1( ...
- 网站优化—MySQL优化
MySQL优化 简介 由于页面静态化技术可以实现对动态数据的缓存,但是有的时候还是需要去请求数据库.所以对数据库的优化也是不可缺少的. 优化思路 设计:存储引擎,字段,范式 自身:索引,自身的缓存 架 ...
- 机器学习(4):数据分析的工具-pandas的使用
前面几节说一些沉闷的概念,你若看了估计已经心生厌倦,我也是.所以,找到了一个理由来说一个有兴趣的话题,就是数据分析.是什么理由呢?就是,机器学习的处理过程中,数据分析是经常出现的操作.就算机器对大量样 ...
- UOJ 外星人
题目: 2044年,Picks建成了人类第一台基于量子理论的银河系信息传递机.Picks游遍了宇宙,雇用了n个外星人来帮他作为信息传递机的中转站.我们将外星人依次编号为1 到n,其中i 号外星人有ai ...
- mysqldump实践
mysqldump mysqldump---逻辑备份,热备 单线程,适合数据量小的库 mysql官方自带的命令行工具 #全库 )mysqldump -uroot -p123456 --sock ...
- 一入python深似海--range()、list与for
range使用方法 使用python的人都知道range()函数非常方便,今天再用到他的时候发现了非常多曾经看到过可是忘记的细节. 这里记录一下: range(1,5)#代表从1到5(不包括5) [1 ...