(翻译)React 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的更多相关文章
- (翻译) Container Components
react.js javascript 这篇文章翻译自Medium的一篇文章:Container Components 选择这篇文章翻译的原因是,在刚接触React的时候,这篇文章很好的指引我了解 ...
- [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 =& ...
- [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 ...
- Tomcat翻译--Context Container
原文:http://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Resource_Definitions The Context Cont ...
- [Redux] Redux: Extracting Container Components -- AddTodo
Code to be refactored: const AddTodo = ({ onAddClick }) => { let input; return ( <div> < ...
- React组件Components的两种表示方式
函数式的表示: function Welcome(props) { return <h1>Hello, {props.name}</h1>; } Class式的表示: clas ...
随机推荐
- ibatis.net 实现多数据库配置
1.1 功能介绍 使用ibatis.net ORM框架时,有时候需要操作多个数据库,同时有时候也需要对连接数据库信息进行加密,本文通过将配置连接写到Web.config中, 这样就可以在Web.co ...
- Mac上安装配置和简单使用PostgreSQL(仍然很不懂)
因为想要使用推荐的rails-template.需要使用postgres.并初始化了一个用户postgres,密码是postgres.( e.g. $ createuser -d postgres ) ...
- ps怎么替换颜色
1.图像--->>调整--->>替换颜色
- vue-cli脚手架目录讲解
build目录 config目录 是webpack配置相关的 node-modules目录 是我们用npm install 安装的依赖代码库 src目录 是我们项目的源码(我们开发的所有项目文件都在s ...
- 学习 Flask 扩展 Flask-RESTful
pip install Flask-RESTful Flask-RESTful扩展.首先,我们来安装上面这个扩展. from flask import Flask from flask_restful ...
- 【转】Selenium2 API详解
[转自]http://blog.csdn.net/wuxuehong0306/article/details/49762961 打开浏览器 Ø 打开firefox浏览器 WebDriver driv ...
- Try Ubuntu Landscape on Xenial (by quqi99)
版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 (http://blog.csdn.net/quqi99) Landscape is used for mana ...
- URL 生成带文字二维码
<!DOCTYPE html> <html> <head> <title></title> <meta charset="u ...
- POJ3696 The Luckiest number
题意 Language:Default The Luckiest number Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7 ...
- 转 HTTP/2: The Long-Awaited Sequel
HTTP/2: The Long-Awaited Sequel Thursday, October 9, 2014 2:01 AM 6 Ready to speed things up? Here a ...