这篇文章翻译自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的更多相关文章

  1. (翻译)React Container Components

    原文:Container Components Container Components 在 React 模式上对我的代码有最深远影响的一个模式叫 container component 模式. 在 ...

  2. [Redux] Extracting Container Components (FilterLink)

    Learn how to avoid the boilerplate of passing the props down the intermediate components by introduc ...

  3. Presentational and Container Components

    https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0 There’s a simple pattern I fi ...

  4. [Redux] Extracting Container Components -- Complete

    Clean TodoApp Component, it doesn't need to receive any props from the top level component: const To ...

  5. [Redux] Extracting Container Components -- VisibleTodoList

    Code to be refacted: const TodoList = ({ todos, onTodoClick }) => ( <ul> {todos.map(todo =& ...

  6. [Redux] Redux: Extracting Container Components -- AddTodo

    Code to be refactored: const AddTodo = ({ onAddClick }) => { let input; return ( <div> < ...

  7. 关于React的Container&Presentational Component模型结构分析

    react.js javascript 3 之前翻译了两篇关于Container&Presentational Component模型的文章,一篇是基础的Container和Component ...

  8. [Angular 2] Passing Observables into Components with Async Pipe

    The components inside of your container components can easily accept Observables. You simply define ...

  9. C#委托,事件理解入门 (译稿)

    原文地址:http://www.codeproject.com/Articles/4773/Events-and-Delegates-Simplified 引用翻译地址:http://www.cnbl ...

随机推荐

  1. Manjaro Linux下使用powerline

    作为linux的重度使用者,vim和bash的使用也非常平凡,总想有点不同,感觉千篇一律的提示符已经看的厌倦了.作为广大网友的推荐的powerline可以为bash和vim的提示符美化增色不少.下面请 ...

  2. 無法使用 system/bin/r 讀取 pmic pm8937 hardware regitster 的原因

    Platform Qualcomm MSM8917 + PM8937 + PMI8940 起因 同事問我 PM8937 的 VREG_L17 如何設定成 3.3V, 從 PM8937 hardware ...

  3. LeetCode OJ-- Populating Next Right Pointers in Each Node

    https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/ 二叉树遍历的变种:树的节点多了个next指针 ...

  4. 网络扫描集成工具SPARTA

    网络扫描集成工具SPARTA   SPARTA是Kali Linux自带的一款图形化网络扫描工具.它集成了NMAP.Nikto.hydra.nbtscan等几十种工具.用户只需要输入要扫描的IP或者I ...

  5. Install Ruby on Rails on Ubuntu 12.04 LTS

    1:Update package repository. sudo apt-get update 2:Install git and Curl.     Git:是一个简单,快速,高效的版本控制系统. ...

  6. Zabbix 企业Nginx监控

    Zabbix监控Nginx状态 1 修改Nginx配置文件,开启Nginx监控 location /nginx_status { stub_status on; access_log off; all ...

  7. Swagger2接口注释参数使用数组

    allowMultiple = true, paramType = "query", dataType = "string" 输出的就是这样的:Array[st ...

  8. 查看网络port占用

    Linux和Mac下通用: 1.  利用 netstat 查看网络状态命令: netstat -an|grep port号 2. 利用list open file 命令打开文件(一切都是文件. 包含网 ...

  9. Eclipse中src/main/resources配置文件启动问题

    项目pom文件有做修改如下的时候,还没有进行mvn clean install 启动test项目中的appcontext会 可以手动清空 然后就可以了. 出现如下问题的原因是 配置文件默认输出到tar ...

  10. android MPChart图标使用具体解释

    近期项目里有要加入更加复杂的图标了,曾经一些简单的曲线图,饼状图.风险指示图等,都是自己画.随着难度的添加.越来越力不从心.曾经研究过achartenginee图标框架,但发现achartengine ...