原文:Container Components

Container Components

在 React 模式上对我的代码有最深远影响的一个模式叫 container component 模式。

在 React.js Conf 上,Jason Bonta 和我们讲了他们在Facebook上是如何建立高性能组件(High Performance Components)。Nestled 在这个演讲中讲的就是this gem about container components

这个概念很简单:

一个 container 只是做数据拉取然后渲染与它的 corresponding 子组件。就是这样。

“Corresponding” 意味着分享同一个名称的组件,例如:

StockWidgetContainer => StockWidget
TagCloudContainer => TagCloud
PartyPooperListContainer => PartyPooperList

这就是其中的概念。

Why containers?

比如你有一个用于展示评论的组件。你并不知道有关 container 组件。所以,你会将所有东西都放在一个地方。

//  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});
}.bind(this)
});
} render(){
return <ul> {this.state.comments.map(renderComponent)} </ul>;
} renderComponent({body, author}){
return <li> {body}-{author} </li>;
}
}

你的组件就是用于拉取数据并展示它。这并没有什么"错误",但是你却错过了一些React的优点。

可复用性

CommentList组件除了在同一精确的条件情况下才能复用。

数据结构

你希望的组件应该规定他们需要的数据类型的预期。PropTypes正是干这个的。

我们的组件对数据结构要求很高但是没有办法说出这些要求。如果json的接口数据改变了,这个组件会不做出任何提示出错。(其实想说的就是,无法好好利用PropTypes来把控数据结构是否正确)

再来一次。这一次加上container

首先,让我们将数据拉取的功能移到 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} />;
}
}

现在,我们将comments作为CommentList的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并且一旦出错便会提示。

我是一个 container components 的大粉丝。他们让我的 React game 进步了很多,并且使我的组件更容易去阅读。尝试一下他们,并看一下Jason的演讲。太棒了!

Carry on, nerds.

(翻译)React Container Components的更多相关文章

  1. (翻译) Container Components

    react.js javascript   这篇文章翻译自Medium的一篇文章:Container Components 选择这篇文章翻译的原因是,在刚接触React的时候,这篇文章很好的指引我了解 ...

  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. [React] Use React.ReactNode for the children prop in React TypeScript components and Render Props

    Because @types/react has to expose all its internal types, there can be a lot of confusion over how ...

  7. Tomcat翻译--Context Container

    原文:http://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Resource_Definitions The Context Cont ...

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

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

  9. React组件Components的两种表示方式

    函数式的表示: function Welcome(props) { return <h1>Hello, {props.name}</h1>; } Class式的表示: clas ...

随机推荐

  1. 转:Tomcat 7.0配置SSL的问题及解决办法

    原文:https://dong-shuai22-126-com.iteye.com/blog/1830209

  2. Bitwise Equations

    Problem Description You are given two positive integers X and K. Return the K-th smallest positive i ...

  3. Android orm 框架xUtils简介

    数据库操作建议用ORM框架,简单高效.这里推荐xUtils,里面包含DBUtils.github地址:https://github.com/wyouflf/xUtils 获得数据库实例建议用单例模式. ...

  4. 物理内存不够用,临时增大Linux交换分区的方法

    当系统的物理内存不够用的时候,就需要将物理内存中的一部分空间释放出来,以供当前运行的程序使用.那些被释放的空间可能来自一些很长时间没有什么 操作的程序,这些被释放的空间被临时保存到Swap空间中,等到 ...

  5. 002——php字符串中的处理函数(一)

    <?php /** * 字符串处理函数: * 一.PHP处理字符串的空格: * strlen 显示字符串长度 * * trim 对字符串左右空格删除: * ltrim 对字符串左侧空格删除 * ...

  6. 基于Open XML 导出数据到Excel

    数据导出的结果: 步骤1.新建一个Excel 文档,模板根据自己需要设置 步骤2.使用OpenXml  打开Excel 文件 步骤3.点击ReflectCode 功能,生成相应的代码文档 using ...

  7. linux提权辅助工具(三):privchecker.py

    来自:https://www.securitysift.com/download/linuxprivchecker.py #!/usr/env python ##################### ...

  8. 20155322 2016-2017-2 《Java程序设计》第6周学习总结

    20155322 2016-2017-2 <Java程序设计>第6周学习总结 教材学习内容总结 第六周学习的主要内容是课本的第十第十一章: 第十章介绍的是输入.输出,Java中的流分为两种 ...

  9. python的pip源在windows和linux修改

    windows和linux修改python的pip源 https://www.cnblogs.com/cwp-bg/p/8497075.html windows和linux修改python的pip源 ...

  10. ssh的时候提示No user exists for uid 501

    原来是oh my zash升级导致的.关闭iterm2然后重新打开iterm2就可以了