当前位置: 首页 > news >正文

Presentational and Container Components

https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0

There’s a simple pattern I find immensely useful when writing React applications. If you’ve been doing React for a while, you have probably already discovered it. This article explains it well, but I want to add a few more points.

You’ll find your components much easier to reuse and reason about if you divide them into two categories. I call them Container and Presentational components* but I also heard Fat and SkinnySmart and Dumb, Stateful and PureScreens and Components, etc. These all are not exactly the same, but the core idea is similar.

My presentational components:

  • Are concerned with how things look.
  • May contain both presentational and container components** inside, and usually have some DOM markup and styles of their own.
  • Often allow containment via this.props.children.
  • Have no dependencies on the rest of the app, such as Flux actions or stores.
  • Don’t specify how the data is loaded or mutated.
  • Receive data and callbacks exclusively via props.
  • Rarely have their own state (when they do, it’s UI state rather than data).
  • Are written as functional components unless they need state, lifecycle hooks, or performance optimizations.
  • Examples: Page, Sidebar, Story, UserInfo, List.

My container components:

  • Are concerned with how things work.
  • May contain both presentational and container components** inside but usually don’t have any DOM markup of their own except for some wrapping divs, and never have any styles.
  • Provide the data and behavior to presentational or other container components.
  • Call Flux actions and provide these as callbacks to the presentational components.
  • Are often stateful, as they tend to serve as data sources.
  • Are usually generated using higher order components such as connect()from React Redux, createContainer() from Relay, or Container.create() from Flux Utils, rather than written by hand.
  • Examples: UserPage, FollowersSidebar, StoryContainer, FollowedUserList.

I put them in different folders to make this distinction clear.

Benefits of This Approach

  • Better separation of concerns. You understand your app and your UI better by writing components this way.
  • Better reusability. You can use the same presentational component with completely different state sources, and turn those into separate container components that can be further reused.
  • Presentational components are essentially your app’s “palette”. You can put them on a single page and let the designer tweak all their variations without touching the app’s logic. You can run screenshot regression tests on that page.
  • This forces you to extract “layout components” such as Sidebar, Page, ContextMenu and use this.props.children instead of duplicating the same markup and layout in several container components.

Remember, components don’t have to emit DOM. They only need to provide composition boundaries between UI concerns.

Take advantage of that.

When to Introduce Containers?

I suggest you to start building your app with just presentational components first. Eventually you’ll realize that you are passing too many props down the intermediate components. When you notice that some components don’t use the props they receive but merely forward them down and you have to rewire all those intermediate components any time the children need more data, it’s a good time to introduce some container components. This way you can get the data and the behavior props to the leaf components without burdening the unrelated components in the middle of the tree.

This is an ongoing process of refactoring so don’t try to get it right the first time. As you experiment with this pattern, you will develop an intuitive sense for when it’s time to extract some containers, just like you know when it’s time to extract a function. My free Redux Egghead series might help you with that too!

Other Dichotomies

It’s important that you understand that the distinction between the presentational components and the containers is not a technical one. Rather, it is a distinction in their purpose.

By contrast, here are a few related (but different!) technical distinctions:

  • Stateful and Stateless. Some components use React setState() method and some don’t. While container components tend to be stateful and presentational components tend to be stateless, this is not a hard rule. Presentational components can be stateful, and containers can be stateless too.
  • Classes and Functions. Since React 0.14, components can be declared both as classes and as functions. Functional components are simpler to define but they lack certain features currently available only to class components. Some of these restrictions may go away in the future but they exist today. Because functional components are easier to understand, I suggest you to use them unless you need state, lifecycle hooks, or performance optimizations, which are only available to the class components at this time.
  • Pure and Impure. People say that a component is pure if it is guaranteed to return the same result given the same props and state. Pure components can be defined both as classes and functions, and can be both stateful and stateless. Another important aspect of pure components is that they don’t rely on deep mutations in props or state, so their rendering performance can be optimized by a shallow comparison in their shouldComponentUpdate() hook. Currently only classes can define shouldComponentUpdate() but that may change in the future.

Both presentational components and containers can fall into either of those buckets. In my experience, presentational components tend to be stateless pure functions, and containers tend to be stateful pure classes. However this is not a rule but an observation, and I’ve seen the exact opposite cases that made sense in specific circumstances.

Don’t take the presentational and container component separation as a dogma. Sometimes it doesn’t matter or it’s hard to draw the line. If you feel unsure about whether a specific component should be presentational or a container, it might be too early to decide. Don’t sweat it!

Example

This gist by Michael Chan pretty much nails it.

Further Reading

  • Getting Started with Redux
  • Mixins are Dead, Long Live Composition
  • Container Components
  • Atomic Web Design
  • Building the Facebook News Feed with Relay

Footnotes

* In an earlier version of this article I called them “smart” and “dumb” components but this was overly harsh to the presentational components and, most importantly, didn’t really explain the difference in their purpose. I enjoy the new terms much better, and I hope that you do too!
** In an earlier version of this article I claimed that presentational components should only contain other presentational components. I no longer think this is the case. Whether a component is a presentational component or a container is its implementation detail. You should be able to replace a presentational component with a container without modifying any of the call sites. Therefore, both presentational and container components can contain other presentational or container components just fine.

相关文章:

  • 使用Python一年多了,总结八个好用的Python爬虫技巧
  • IO的模式
  • [cogs2652]秘术「天文密葬法」
  • 【AliOS Things学习笔记】在Developerkit开发板上运行blink例程
  • 黑盒测试的测试方法
  • 开发阶段
  • angular2+ 生命周期
  • 可见面判别算法---光线投射算法
  • [每日短篇] 10 - Docker 清理无用的镜像
  • 书摘—极致产品
  • 0013-如何在Kerberos与非Kerberos的CDH集群BDR不可用时复制数据
  • MySQL数据类型详解
  • React和Redux的连接react-redux
  • VUE-文字跑马灯
  • 虚拟机与Docker有何不同?
  • [译]CSS 居中(Center)方法大合集
  • 2018一半小结一波
  • css选择器
  • egg(89)--egg之redis的发布和订阅
  • Java多线程(4):使用线程池执行定时任务
  • October CMS - 快速入门 9 Images And Galleries
  • php ci框架整合银盛支付
  • Python_OOP
  • vue2.0一起在懵逼的海洋里越陷越深(四)
  • vue的全局变量和全局拦截请求器
  • 阿里云前端周刊 - 第 26 期
  • 技术攻略】php设计模式(一):简介及创建型模式
  • 前嗅ForeSpider中数据浏览界面介绍
  • 思考 CSS 架构
  • 云大使推广中的常见热门问题
  • 【运维趟坑回忆录 开篇】初入初创, 一脸懵
  • MyCAT水平分库
  • ​​​​​​​ubuntu16.04 fastreid训练过程
  • ​iOS安全加固方法及实现
  • ​软考-高级-系统架构设计师教程(清华第2版)【第15章 面向服务架构设计理论与实践(P527~554)-思维导图】​
  • #HarmonyOS:Web组件的使用
  • #Ubuntu(修改root信息)
  • (0)Nginx 功能特性
  • (13)Hive调优——动态分区导致的小文件问题
  • (Bean工厂的后处理器入门)学习Spring的第七天
  • (C++20) consteval立即函数
  • (C语言版)链表(三)——实现双向链表创建、删除、插入、释放内存等简单操作...
  • (echarts)echarts使用时重新加载数据之前的数据存留在图上的问题
  • (LeetCode 49)Anagrams
  • (二)学习JVM —— 垃圾回收机制
  • (翻译)terry crowley: 写给程序员
  • (附源码)spring boot网络空间安全实验教学示范中心网站 毕业设计 111454
  • (六)什么是Vite——热更新时vite、webpack做了什么
  • (每日持续更新)jdk api之StringBufferInputStream基础、应用、实战
  • (求助)用傲游上csdn博客时标签栏和网址栏一直显示袁萌 的头像
  • (转) Android中ViewStub组件使用
  • (转)jQuery 基础
  • (转)linux 命令大全
  • .bat批处理(五):遍历指定目录下资源文件并更新
  • .chm格式文件如何阅读